Reputation: 422
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 ?
Upvotes: 1
Views: 1120
Reputation: 3307
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:
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.
}
});
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.
Upvotes: 0