Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use setImageUri() on Android

Can you help me please? I've tried :

ImageButton imgbt=(ImageButton)findViewById(R.id.imgbutton);
Uri imgUri=Uri.parse("/data/data/MYFOLDER/myimage.png");
imgbt.setImageUri(imgUri);

but I didn't see anything, simply a void button.

like image 917
ilredelweb Avatar asked Oct 06 '10 08:10

ilredelweb


People also ask

What is setImageUri?

ImageView. setImageUri only works for local Uri, ie a reference to a local disk file, not a URL to an image on the network. Here is an example of how to fetch a Bitmap from the network.

What is image view in android?

ImageView class is used to display any kind of image resource in the android application either it can be android. graphics. Bitmap or android. graphics. drawable.

What is a ImageView?

Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.


4 Answers

ImageView.setImageUri only works for local Uri, ie a reference to a local disk file, not a URL to an image on the network.

Here is an example of how to fetch a Bitmap from the network.

    private Bitmap getImageBitmap(String url) {
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
       } catch (IOException e) {
           Log.e(TAG, "Error getting bitmap", e);
       }
       return bm;
    } 

Once you have the Bitmap from getImageBitmap(), use: imgView.setImageBitmap(bm);

like image 193
SoH Avatar answered Oct 24 '22 15:10

SoH


It should be

Uri imgUri=Uri.parse("file:///data/data/MYFOLDER/myimage.png");
like image 30
bhups Avatar answered Oct 24 '22 17:10

bhups


How about this one:

Bitmap bitmap = BitmapFactory.decodeFile(fullFileName);
imgProfileImage.setImageBitmap(bitmap);
like image 44
mehrdad seyrafi Avatar answered Oct 24 '22 15:10

mehrdad seyrafi


Its best to avoid building the path by hand, try :

imgbt.setImageUri(Uri.fromFile(new File("/data/data/....")));
like image 22
vine'th Avatar answered Oct 24 '22 17:10

vine'th