开发者

Get image from web with URI

What is wrong with this code. It does not give an error but no image can be seen

ImageView a= (ImageView) findViewById(R.id.imageView1);

String imageBaseDirectory = "www.dh开发者_StackOverflow社区a.com.tr/newpics/news/";
String imageName = "230620111356175716857.jpg";
a.setImageURI(Uri.parse(imageBaseDirectory+imageName)); 


First, you should add http:// at the beginning of you URI:

Uri uri = Uri.parse("http://www.dha.com.tr/newpics/news/230620111356175716857.jpg");

ImageView does not support loading images from remote locations. Only resources or files on local file system. In your case you should see warning in LogCat:

06-23 15:19:52.487: WARN/System.err(27097): java.io.FileNotFoundException: /http:/www.dha.com.tr/newpics/news/230620111356175716857.jpg (No such file or directory)
06-23 15:19:52.503: WARN/System.err(27097):     at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
06-23 15:19:52.503: WARN/System.err(27097):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
06-23 15:19:52.503: WARN/System.err(27097):     at java.io.FileInputStream.<init>(FileInputStream.java:82)
06-23 15:19:52.503: WARN/System.err(27097):     at java.io.FileInputStream.<init>(FileInputStream.java:134)
06-23 15:19:52.503: WARN/System.err(27097):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:349)
06-23 15:19:52.503: WARN/System.err(27097):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:399)
06-23 15:19:52.503: WARN/System.err(27097):     at android.graphics.drawable.Drawable.createFromPath(Drawable.java:801)
06-23 15:19:52.503: WARN/System.err(27097):     at android.widget.ImageView.resolveUri(ImageView.java:516)
06-23 15:19:52.503: WARN/System.err(27097):     at android.widget.ImageView.setImageURI(ImageView.java:293)
...
06-23 15:19:52.503: WARN/System.err(27097):     at dalvik.system.NativeStart.main(Native Method)
06-23 15:19:52.503: INFO/System.out(27097): resolveUri failed on bad bitmap uri: http://www.dha.com.tr/newpics/news/230620111356175716857.jpg

You should download the image yourself and then specify local path for setImageURI.


Try using

a.setImageBitmap(BitmapFactory.decodeStream(new URL(imageBaseDirectory+imageName).openConnection().getInputStream()));

as suggested in the Android documentation.


You should have posted here the Logcat output so that we may come to know about the possible problems.

But still you may forget to add INTERNET permission inside the AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>


Uri.parse needs an encoded URI string so I think you have to add the scheme.

String imageBaseDirectory = "http://www.dha.com.tr/newpics/news/";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜