<main>
<article class="baidu_pl">
一.直接上代碼(四種來源途徑下加載圖片)
1
package com.zhc.picasso;
import java.io.File;
import com.squareup.picasso.Picasso;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.widget.ImageView;
public class MainActivity extends Activity {
public static final String ANDROID_RESOURCE = "android.resource://";
public static final String FOREWARD_SLASH = "/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1.從安卓項目資源加載
ImageView targetImageView1 = (ImageView) findViewById(R.id.imageview1);
int resourceId = R.drawable.ic_launcher;
Picasso.with(getBaseContext()).load(resourceId).into(targetImageView1);
//2.從本地文件中加載
ImageView targetImageView2 = (ImageView) findViewById(R.id.imageview2);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"zhc.png");
Picasso.with(getBaseContext()).load(file).into(targetImageView2);
// 3.從網(wǎng)絡上加載圖片
ImageView targetImageView3 = (ImageView) findViewById(R.id.imageview3);
String internetUrl =
"http://img03.sogoucdn.com/app/a/100520093/508de2a2df69f20f-ad54974be9c2600f-01f80ad8e3e76b8fe0205b58d9e59649.jpg";
Picasso.with(getBaseContext()).load(internetUrl).into(targetImageView3);
// 備用網(wǎng)絡路徑:http://img03.sogoucdn.com/app/a/100520093/508de2a2df69f20f-ad54974be9c2600f-01f80ad8e3e76b8fe0205b58d9e59649.jpg
//百度圖片路徑(這個應該是圖片在網(wǎng)頁上的路徑,而不是圖片路徑):http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E5%9B%BE%E7%89%87&hs=0&pn=1&spn=0&di=71162346090&pi=0&rn=1&tn=baiduimagedetail&ie=utf-8&oe=utf-8&cl=2&lm=-1&cs=4155302816%2C1201715785&os=159233822%2C272436753&simid=3235028088%2C3077875656&adpicid=0&ln=30&fr=ala&fm=&sme=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fpic33.nipic.com%2F20130916%2F3420027_192919547000_2.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bgtrtv_z%26e3Bv54AzdH3Fzi7wgptAzdH3Fb80b88_8l_z%26e3Bip4s&gsm=0
// 4.從URI加載圖片(這里用資源文件轉換為URI)
Uri uri = resourceIdToUri(getBaseContext(), R.drawable.ic_launcher);
ImageView targetImageView4 = (ImageView) findViewById(R.id.imageview4);
Picasso.with(getBaseContext()).load(uri).into(targetImageView4);
}
private static Uri resourceIdToUri(Context context, int resourceId) {
return Uri.parse(ANDROID_RESOURCE + context.getPackageName()
+ FOREWARD_SLASH + resourceId);
}
}
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/imageview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/imageview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/imageview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
3》權限一定要記得加
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
4》結果顯示:
5》picasso-2.5.2.jar的下載路徑:https://github.com/square/picasso
然后復制到工程的libs目錄下唬涧。
二才沧、四種來源途徑下加載圖片遇到的問題:
1.從Android Resources 中加載(即Android項目資源中):
加載很順利沒有遇到問題氧急。
2.從本地文件中加載:
1》在小米手機上運行時會報Permisson denied (拒絕訪問)的問題,加載不到圖片凑耻;這問題還沒有解決纳令,不知到是不是小米系統(tǒng)對此做了限制的原因。
2》一定要記得加讀或者寫的權限进栽,網(wǎng)絡訪問的權限對于此途徑就沒有必要添加了德挣。
3》在公司自己做的安卓平臺上,將圖片zhc.png放在平臺設備下的Pictures目錄下(不放在此目錄下訪問不到)快毛;因為Environment.DIRECTORY_PICTURES決定了訪問目錄必須為Pictures目錄 格嗅,這樣就可以加載到圖片了番挺。 關于Environment類的更多信息訪問:http://blog.csdn.net/barnett_zhubo/article/details/6832832
4》在原生模擬器上沒有找到Pictures目錄,所以在此加載不到圖片屯掖。
3.從網(wǎng)絡上加載:
1》圖片路徑為一張百度網(wǎng)頁上的圖片時玄柏,手機瀏覽器在此路徑下問找到圖片,但是app demo中卻找不到贴铜;報的錯誤open failed:EBUSY(Device or resource busy)設備或資源忙禁荸。(這個圖片的路徑應該有問題:這個應該是圖片在網(wǎng)頁上的路徑,而不是圖片路徑)
但是換個路徑在手機和模擬器上的app均能加載到圖片阀湿。
4.從URI上加載圖片:
沒有遇到問題。
</article>
</main>