1

Open Gallery Intent

 Intent galleryIntent = new Intent( Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        galleryIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivityForResult(galleryIntent, FILE_REQUEST);
           

OnActivity Result

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
     selected_photo = data.getData();

     String[] filePath = {MediaStore.Images.Media.DATA};
     Cursor c = getContentResolver().query(selected_photo, filePath,null, null, null);
     c.moveToFirst();
     int columnIndex = c.getColumnIndex(filePath[0]);
     picturePath = c.getString(columnIndex);
     c.close();

     Intent i = new Intent(First.this, Second.class);
     i.putExtra("pic", picturePath);
     startActivity(i);
  }

First Activity Log

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: 46: open failed: ENOENT (No such file or directory)

On Next Activity, i am getting error inside this function

public Bitmap decodeBitmapFromPath(String filePath) {
    Bitmap scaledBitmap = null;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    scaledBitmap = BitmapFactory.decodeFile(filePath, options);

    options.inSampleSize = calculateInSampleSize(options, 30, 30);
    options.inDither = false;
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inJustDecodeBounds = false;

    scaledBitmap = BitmapFactory.decodeFile(filePath, options);

    ExifInterface exif;
    try {
        exif = new ExifInterface(filePath);

        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION, 0);
        Matrix matrix = new Matrix();
        if (orientation == 6) {
            matrix.postRotate(90);
        } else if (orientation == 3) {
            matrix.postRotate(180);
        } else if (orientation == 8) {
            matrix.postRotate(270);
        }
        scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
                scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,
                true);

        eW = scaledBitmap.getWidth();
        eH = scaledBitmap.getHeight();
        eM = matrix;

    } catch (IOException e) {
        e.printStackTrace();
        FirebaseCrashlytics.getInstance().recordException(e);
    }
    return scaledBitmap;
}

Second Activity Log

java.io.FileNotFoundException: 46: open failed: ENOENT (No such file or directory) at libcore.io.IoBridge.open(IoBridge.java:492) at java.io.FileInputStream.(FileInputStream.java:160) at java.io.FileInputStream.(FileInputStream.java:115) at android.media.ExifInterface.initForFilename(ExifInterface.java:2531) at android.media.ExifInterface.(ExifInterface.java:1500) at .SecondActivity.decodeBitmapFromPath(SecondActivity.java:889)

Now, how can i access this photo in next activity as bitmap , and apply different effects on it ?

Umeshkumar D.
  • 412
  • 4
  • 18
  • how can i access bitmap from that uri ? – Umeshkumar D. Oct 31 '20 at 09:01
  • how to access that file ? – Umeshkumar D. Oct 31 '20 at 09:04
  • check my question, i have added more code.. @blackapps – Umeshkumar D. Oct 31 '20 at 09:18
  • 1
    `String[] filePath = {MediaStore.Images.Media.DATA` The .DATA column is not available on Android 10. Moreover you do not need a file path as you can access the file directly using the uri. Just open an inputstream for the uri and then use BitmapFactory.decodeStream(). Just try to create that bitmap in onActivityResult first. To see that it works. Do not try it in a different activity yet as it will not yet work probably. – blackapps Oct 31 '20 at 09:26
  • bitmap created in onActivityResult() using InputStream, now how to work with second activity ? @blackapps – Umeshkumar D. Oct 31 '20 at 09:48
  • 1
    In the same way. Just put `data.getData().toString()` as putExtra in the intent to start that activity. Retrieve the string in next activity and parse it to uri back. Probably it does not work there yet but then i hear from you. – blackapps Oct 31 '20 at 10:06

1 Answers1

0

If you want to pick the image from gallery, then try this new API. It's called ActivityResultLauncher

You don't have to remember any request code to get result and it's easy to use

Follow these steps to access the image from the storage and convert it to bitmap:

  1. First create the ActivityResultLauncher like this :

    ActivityResultLauncher<String> mGetContent = 
             registerForActivityResult(new GetContent(), new ActivityResultCallback<Uri>() {
     @Override
     public void onActivityResult(Uri uri) {
       // Handle the returned Uri, We will get a bitmap from it here.
    
     }
    });
    
  2. Then, launch the system image picker on button click or wherever you want.

    mGetContent.launch("image/*");
    
  • Bitmap from Uri method.

    public statice Bitmap getBitmapFromUri(Context ctx, Uri uri) {
         try {
              if (uri != null) {
                  Bitmap bitmap =
                      MediaStore.Images.Media.getBitmap(ctx.getContentResolver(), uri);
              }
          } catch (Exception e) {
              //handle exception
          }
      }
    

Now you can use the bitmap anywhere you want.

Dev4Life
  • 2,328
  • 8
  • 22
  • If you want to get file path from Uri, then just google it. It's easy to implement & you will get the path from uri. – Dev4Life Aug 03 '21 at 14:48
  • will this work for all API levels without any problem or it is API specific like only for API 10/11?? – Vivek Thummar Sep 30 '21 at 06:09
  • and do we need query parameter in manifest file?? like shown here - https://stackoverflow.com/a/64853500/10752962 – Vivek Thummar Sep 30 '21 at 06:11
  • and what about if i get error how can i know in `onActivityResult()` because if i comes back without selecting image, definitely result uri will be null and also same if i gets any error – Vivek Thummar Sep 30 '21 at 06:40
  • @VivekThummar No, It will work only below API 29. For Android 10 above, you can't directly access the media from file path, So you have to use Mediastore API. It will give you the uri of selected Media & hence, you'll have to find a way to use the "uri" directly for your media related operations. It's not that hard though. There are many easy tutorials out there. – Dev4Life Sep 30 '21 at 12:34
  • Check if "uri" is not null then proceed ahead. That way, you won't get any problem because of null uri. – Dev4Life Sep 30 '21 at 12:35