21

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.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
ilredelweb
  • 3,091
  • 4
  • 17
  • 14

7 Answers7

18

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);

Ribo
  • 3,363
  • 1
  • 29
  • 35
SoH
  • 2,180
  • 2
  • 24
  • 53
  • thanks, this `getImageBitmap` method works for me. Far better than using `setImageURI` – Solostaran14 Nov 06 '12 at 22:07
  • @iredelweb should really approve this answer. – Max Aug 21 '15 at 12:16
  • This doesn't answer the question, about how to make .setImageUri to work. It just shows how to read a bitmap from the network. How does that make an image appear in an ImageView ? – Ribo Jun 03 '19 at 16:38
8

It should be

Uri imgUri=Uri.parse("file:///data/data/MYFOLDER/myimage.png");
bhups
  • 14,345
  • 8
  • 49
  • 57
4

How about this one:

Bitmap bitmap = BitmapFactory.decodeFile(fullFileName);
imgProfileImage.setImageBitmap(bitmap);
mehrdad seyrafi
  • 3,084
  • 2
  • 19
  • 16
2

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

imgbt.setImageUri(Uri.fromFile(new File("/data/data/....")));
vine'th
  • 4,890
  • 2
  • 27
  • 27
1

I also met this issue, it did not show anything. I saw something like this in android developer. It didn't use setImageURI.

private Bitmap getBitmapFromUri(Uri uri, Context context) throws IOException {
    ParcelFileDescriptor parcelFileDescriptor =
            context.getContentResolver().openFileDescriptor(uri, "r");
    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
    Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
    parcelFileDescriptor.close();
    return image;
}

Just for your reference.

kose livs
  • 339
  • 3
  • 5
0
 String imgPath = Environment.getDataDirectory() + "/data/com.wariyum.signage/files/"+ "221215-085656.619.72.jpg";

    //Following lines was expected work, but don't really show up image always - no idea why
    //imgZoom.setImageURI(Uri.parse(imgPath)); 

    //following works always perfectly
            File imgFile = new  File(imgPath);
            if(imgFile.exists()){
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                imgZoom.setImageBitmap(myBitmap);
            }
Nazeel
  • 326
  • 3
  • 10
0

I solved it with framework. Added this line into the gradle:

implementation 'com.facebook.fresco:fresco:1.8.0'

Init singlton in application-class(or another main class in your app)

Fresco.initialize(applicationContext)

And in finish, use it.

XML:

 <com.facebook.drawee.view.SimpleDraweeView
  android:id="@+id/avatar"
  android:layout_width="110dp"
  android:layout_height="110dp" /> 

Java:

avatar.setImageURI(user.getAvatarUrl())
Georgiy Chebotarev
  • 1,676
  • 3
  • 14
  • 18