我正在從url下載圖像,但下載完成後圖像未更改。 我在下面輸入代碼,有人遇到過相同的問題嗎?從url上查看Android圖像
Java
文件
public class MyImgActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imgView =(ImageView)findViewById(R.id.imageView1);
Drawable drawable = LoadImageFromWebOperations("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");
imgView.setImageDrawable(drawable);
}
private Drawable LoadImageFromWebOperations(String url) {
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}
XML
文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_height="match_parent"
android:layout_width="match_parent"></ImageView>
</LinearLayout>
Manifest
文件
<uses-permission android:name="android.permission.INTERNET"/>
你是什麼意思「改變圖像URL不顯示圖像」?你能改述一下嗎? –
你有沒有嘗試加載jpg而不是png? – 2012-08-29 07:38:27
我改變了「http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png」此網址,但不顯示圖像 – Kumar