How to get Bitmap from an Uri?

前端 未结 16 1128
时光说笑
时光说笑 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 10:54

    Inset of getBitmap which is depricated now I use the following approach in Kotlin

    PICK_IMAGE_REQUEST ->
        data?.data?.let {
            val bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(it))
            imageView.setImageBitmap(bitmap)
        }
    
    0 讨论(0)
  • 2020-11-22 10:56

    I have try a lot of ways. this work for me perfectly.

    If you choose pictrue from Gallery. You need to be ware of getting Uri from intent.clipdata or intent.data, because one of them may be null in different version.

      private fun onChoosePicture(data: Intent?):Bitmap {
            data?.let {
                var fileUri:Uri? = null
    
                  data.clipData?.let {clip->
                      if(clip.itemCount>0){
                          fileUri = clip.getItemAt(0).uri
                      }
                  }
                it.data?.let {uri->
                    fileUri = uri
                }
    
    
                   return MediaStore.Images.Media.getBitmap(this.contentResolver, fileUri )
    }
    
    0 讨论(0)
  • 2020-11-22 10:57
    try
    {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(c.getContentResolver() , Uri.parse(paths));
    }
    catch (Exception e) 
    {
        //handle exception
    }
    

    and yes path must be in a format of like this

    file:///mnt/sdcard/filename.jpg

    0 讨论(0)
  • 2020-11-22 10:59

    You can retrieve bitmap from uri like this

    Bitmap bitmap = null;
    try {
        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    0 讨论(0)
  • 2020-11-22 11:02

    Use startActivityForResult metod like below

            startActivityForResult(new Intent(Intent.ACTION_PICK).setType("image/*"), PICK_IMAGE);
    

    And you can get result like this:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case PICK_IMAGE:
                Uri imageUri = data.getData();
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                } catch (IOException e) {
                    e.printStackTrace();
                }
             break;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 11:03

    Here's the correct way of doing it, keeping tabs on memory usage as well:

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == RESULT_OK)
      {
        Uri imageUri = data.getData();
        Bitmap bitmap = getThumbnail(imageUri);
      }
    }
    
    public static Bitmap getThumbnail(Uri uri) throws FileNotFoundException, IOException{
      InputStream input = this.getContentResolver().openInputStream(uri);
    
      BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
      onlyBoundsOptions.inJustDecodeBounds = true;
      onlyBoundsOptions.inDither=true;//optional
      onlyBoundsOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//optional
      BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
      input.close();
    
      if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1)) {
        return null;
      }
    
      int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth;
    
      double ratio = (originalSize > THUMBNAIL_SIZE) ? (originalSize / THUMBNAIL_SIZE) : 1.0;
    
      BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
      bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
      bitmapOptions.inDither = true; //optional
      bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//
      input = this.getContentResolver().openInputStream(uri);
      Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
      input.close();
      return bitmap;
    }
    
    private static int getPowerOfTwoForSampleRatio(double ratio){
      int k = Integer.highestOneBit((int)Math.floor(ratio));
      if(k==0) return 1;
      else return k;
    }
    

    The getBitmap() call from Mark Ingram's post also calls the decodeStream(), so you don't lose any functionality.

    References:

    • Android: Get thumbnail of image on SD card, given Uri of original image

    • Handling large Bitmaps

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