How to get Bitmap from an Uri?

前端 未结 16 1127
时光说笑
时光说笑 2020-11-22 10:41

How to get a Bitmap object from an Uri (if I succeed to store it in /data/data/MYFOLDER/myimage.png or file///data/data/MYFOLDER/myimage.png) to u

相关标签:
16条回答
  • 2020-11-22 11:04

    . . IMPORTANT: See answer from @Mark Ingram below and @pjv for at better solution. . .

    You could try 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;
    }
    

    But remember, this method should only be called from within a thread (not GUI -thread). I a AsyncTask.

    0 讨论(0)
  • 2020-11-22 11:04

    Full method to get image uri from mobile gallery.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
      if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
                    Uri filePath = data.getData();
    
         try { //Getting the Bitmap from Gallery
               Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
               rbitmap = getResizedBitmap(bitmap, 250);//Setting the Bitmap to ImageView
               serImage = getStringImage(rbitmap);
               imageViewUserImage.setImageBitmap(rbitmap);
          } catch (IOException e) {
               e.printStackTrace();
          }
    
    
       }
    }
    
    0 讨论(0)
  • 2020-11-22 11:07
      InputStream imageStream = null;
        try {
            imageStream = getContext().getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
    
    0 讨论(0)
  • 2020-11-22 11:08

    Here's the correct way of doing it:

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK)
        {
            Uri imageUri = data.getData();
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
        }
    }
    

    If you need to load very large images, the following code will load it in in tiles (avoiding large memory allocations):

    BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(myStream, false);  
    Bitmap region = decoder.decodeRegion(new Rect(10, 10, 50, 50), null);
    

    See the answer here

    0 讨论(0)
提交回复
热议问题