-1

I am trying to get the image I am getting back at onActivityResult after picking an image at ACTION_GET_CONTENT system intent. I am using the doc's 'setPic()' method in order to reduce the image size but for some reason I get nothing when using the method. Here are my Intent, onActivityResulty and setPic() methods -


private void requestPickingPhoto() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, PICK_VIDEO_REQUEST_CODE);
  }


  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_VIDEO_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
      Uri originalDataUri = data.getData();

      cachedLocalImageUri = originalDataUri;
      Timber.d("Receive image from: %s", originalDataUri);
      mImageSent.setImageURI(cachedLocalImageUri);
      setPic(originalDataUri.toString(), mImageSent);
    }
  }

  private void setPic(String imagePath, ImageView destination) {
    int targetW = destination.getWidth();
    int targetH = destination.getHeight();
    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = (int) Math.sqrt(Math.min(photoW/targetW, photoH/targetH));

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;

    Bitmap bitmap = BitmapFactory.decodeFile(imagePath, bmOptions);
    destination.setImageBitmap(bitmap);
  }


my goal is to use the Uri I am getting back from the intent in order to reduce the image resolution dramaticly so I can use it for firebase cloud messaging rich notification which is size limited.

Alon Shlider
  • 1,187
  • 1
  • 16
  • 46

1 Answers1

1

originalDataUri.toString() will not give you the file path.
Therefore you can't BitmapFactory.decodeFile(imagePath, bmOptions);. Instead you can use BitmapFactory.decodeStream(InputStream is), once you open a stream from Uri:

Change parameter String imagePath of your setPic method to Uri uri, then instead of

BitmapFactory.decodeFile(imagePath, bmOptions);

do this:

InputStream is = context.getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(is, null, bmOptions);

And the same in the end of the method: instead of

Bitmap bitmap = BitmapFactory.decodeFile(imagePath, bmOptions);

do

Bitmap bitmap = BitmapFactory.decodeStream(is, null, bmOptions);

Note, you need a valid Context for that.

Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
  • can you please post the entire setPic() method the way I should implement it? I don't understand at what point should I put your changes and what do they actually differ – Alon Shlider Sep 02 '19 at 06:27
  • Alright, now that I have an imageview with updated bitmap I need the new URI of that image, how do I get it from ? – Alon Shlider Sep 02 '19 at 07:03
  • 2
    that is totally different question. You first need to save it to a file – Vladyslav Matviienko Sep 02 '19 at 07:04
  • Can you please add the relevant code for saving it to a file and getting the uri out of it? tried a few things that did not work – Alon Shlider Sep 02 '19 at 07:15
  • Sorry, I can't as it is a totally different question. You should not ask (as well as I should not answer) multiple question in a single question. Ask a separate question if you have another question. – Vladyslav Matviienko Sep 02 '19 at 07:32