Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ImageView.setImageURI(Uri uri) work with remote files?

Tags:

android

Is it possible to load an image from a remote server using ImageView.setImageURI(Uri uri)?

like image 535
Emanuil Rusev Avatar asked Jun 19 '10 17:06

Emanuil Rusev


2 Answers

The short answer is no! It can't.

You could use ImageView.setImageURI(Uri uri) for instance if the uri contains a reference to a local file. Eg: file:///sdcard/images/thumb.png

like image 81
Francesco Laurita Avatar answered Oct 01 '22 17:10

Francesco Laurita


To load an image from a directory, it should be converted to a Drawable first. Here is a piece of code which can help:

File file = new File ("/sdcard/1.jpg");

ImageView imageView = (ImageView) findViewById(R.id.icon);

imageView.setImageDrawable(Drawable.createFromPath(file.getAbsolutePath()));

Be warned that there is another method for ImageView called setImageURI(URI uri). This method is used to load external files; it doesn't work with the type File. For example, this code won't work:

File file = new File ("/sdcard/1.jpg");

ImageView imageView = (ImageView) findViewById(R.id.icon);

imageView.setImageURI(Uri.fromFile(file));

Thanks to Martin Wibbels for this post.

like image 25
jiahao Avatar answered Oct 01 '22 17:10

jiahao