To extract bitmaps from Uri on Android

Asked 1 years ago, Updated 1 years ago, 70 views

How do I get a bitmap object from Uri? If you receive it and save the bitmap, you can save it by /data/data/MYFOLDER/myimage.png or file///data/MYFOLDER/myimage.png I think we'll use these two paths.

I've tried many different methods, but I can't. Does anyone know?

android bitmap android-image uri

2022-09-22 21:28

1 Answers

Do it like this.

public Bitmap loadBitmap(String url)
{
    Bitmap bm = null;
    InputStream is = null;
    BufferedInputStream bis = null;
    try 
    {
        URLConnection conn = new URL(url).openConnection();
        conn.connect();
        is = conn.getInputStream();
        bis = new BufferedInputStream(is, 8192);
        bm = BitmapFactory.decodeStream(bis);
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    finally {
        if (bis != null) 
        {
            try 
            {
                bis.close();
            }
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
        if (is != null) 
        {
            try 
            {
                is.close();
            }
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    }
    return bm;
}

However, you have to turn it into a thread like AsyncTask.


2022-09-22 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.