Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bad bitmap error when setting Uri

Tags:

android

I would like to make an ImageView display an image on a website. So I create a new ImageView and execute imgView.setImageURI(uri);

When I launch the app the image does not appear and I get the error, "resolveUri failed on bad Bitmap Uri (uri)".

Any ideas on how to solve this?

like image 559
retnuh Avatar asked Sep 10 '10 01:09

retnuh


3 Answers

If it's not a content URI, this link may help. It seems to indicate that imgView.setImageURI() should not be used for regular URIs. Copying in the relevant bit:

Yes, ImageView.setImageURI(ContentURI uri) works, but it is for content URIs particular to the Android platform, not URIs specifying Internet resources. The convention is applied to binary objects (images, for example) which cannot be exposed directly through a ContentProvider's Cursor methods. Instead, a String reference is used to reference a distinct content URI, which can be resolved by a separate query against the content provider. The setImageURI method is simply a wrapper to perform those steps for you.

I have tested this usage of setImageView, and it does work as expected. For your usage, though, I'd look at BitmapFactory.decodeStream() and URL.openStream().

Also to make this answer self-contained, the sample code from another post at that link, showing how to do it:

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

I haven't tested this code, I'm just paranoid and like to ensure SO answers are useful even if every other site on the net disappears :-)

like image 121
paxdiablo Avatar answered Nov 12 '22 03:11

paxdiablo


Glide is an awesome library for displaying images!

 Glide.with(context)
                    .load(uri)
                    .into(imageView);
like image 7
Efi G Avatar answered Nov 12 '22 02:11

Efi G


You need to download the image and then set it as bitmap. Here is one of the many examples: http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

like image 6
Alex Volovoy Avatar answered Nov 12 '22 03:11

Alex Volovoy