You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This solution might not be feasible for every use-cases, and migrating to the latest library (see #858) my be the best solution. But I'm sharing my approach anyways.
The problem I faced: My objective was to select a photo from Gallery, load it in the image cropper and finally save the cropped photo. But I encountered exception in Android 11+ devices while loading the image in cropper.
The solution I used: Instead of loading the selected image directly into the cropper, I created a copy of the image file and loaded the copied file into the image-cropper.
I used ActivityResultLauncher to select the image from gallery. I created a util class named MyFileUtils for my own convenience. I'll include the snippets of relevant methods of those classes below it. I created an activity named EditImageActivity where I load the copied file into the image cropper. Here's how I created the ActivityResultLauncher to select image from gallery:
privateActivityResultLauncher<Intent> galleryActivityLauncher = registerForActivityResult(newActivityResultContracts.StartActivityForResult(), result -> {
// the datatype of result is androidx.activity.result.ActivityResultif (result.getResultCode() == Activity.RESULT_OK) {
Intentdata = result.getData();
if (data == null) {
Log.d(TAG, "initializeActivityLaunchers: intent data is null");
return;
}
UriselectedImageUri = data.getData();
if (null != selectedImageUri) {
// Get the path from the UriFilecopyOfSelectedFile = null;
try {
InputStreaminputStream = getContentResolver().openInputStream(selectedImageUri);
copyOfSelectedFile = MyFileUtils.createImageFile(DoubtHistoryActivity.this);
MyFileUtils.copyInputStreamToFile(inputStream, copyOfSelectedFile);
// refer to the following code snippet for implementation of MyFileUtils' methods
} catch (FileNotFoundExceptione) {
e.printStackTrace();
} catch (IOExceptione) {
e.printStackTrace();
return;
}
selectedImageUri = Uri.fromFile(copyOfSelectedFile);
Intentintent = newIntent(TheCurrentActivity.this, EditImageActivity.class);
intent.putExtra("FILE_PATH", selectedImageUri.toString())
.putExtra(...);
startActivity(intent);
finish();
} else {
Log.d(TAG, "initializeActivityLaunchers: selected image uri is null");
// maybe the user returned from gallery without choosing any image
}
}
});
The following snippet contains implementation of MyFileUtils.createImageFile(..) and MyFileUtils.copyInputStreamToFile(..):
publicstaticFilecreateImageFile(ActivityassociatedActivity) throwsIOException {
// Create an image file nameStringtimeStamp = newSimpleDateFormat("yyyyMMdd_HHmmss").format(newDate());
StringimageFileName = "Question_" + timeStamp + "_";
FilestorageDir = associatedActivity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Log.d(TAG, "createImageFile: storageDir: " + storageDir.getAbsolutePath());
// Save a file: path for use with ACTION_VIEW intentsFiletemporaryFile = File.createTempFile(
imageFileName, /* prefix */".png", /* suffix */// ".jpg", /* suffix */// ".jpeg", /* suffix */storageDir/* directory */
);
temporaryFile.deleteOnExit();
returntemporaryFile;
}
// Copy an InputStream to a File.publicstaticvoidcopyInputStreamToFile(InputStreamin, Filefile) {
OutputStreamout = null;
try {
out = newFileOutputStream(file);
byte[] buf = newbyte[1024];
intlen;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (Exceptione) {
e.printStackTrace();
} finally {
// Ensure that the InputStreams are closed even if there's an exception.try {
if (out != null) {
out.close();
}
// If you want to close the "in" InputStream yourself then remove this// from here but ensure that you close it yourself eventually.in.close();
} catch (IOExceptione) {
e.printStackTrace();
}
}
}
Inside the EditImageActivity, I have the path of the copied image file. So I convert the path string to path Uri and load it in the image cropper in the following way:
This solution might not be feasible for every use-cases, and migrating to the latest library (see #858) my be the best solution. But I'm sharing my approach anyways.
The problem I faced: My objective was to select a photo from Gallery, load it in the image cropper and finally save the cropped photo. But I encountered exception in Android 11+ devices while loading the image in cropper.
The solution I used: Instead of loading the selected image directly into the cropper, I created a copy of the image file and loaded the copied file into the image-cropper.
I used ActivityResultLauncher to select the image from gallery. I created a util class named
MyFileUtils
for my own convenience. I'll include the snippets of relevant methods of those classes below it. I created an activity namedEditImageActivity
where I load the copied file into the image cropper. Here's how I created the ActivityResultLauncher to select image from gallery:The following snippet contains implementation of
MyFileUtils.createImageFile(..)
andMyFileUtils.copyInputStreamToFile(..)
:Inside the
EditImageActivity
, I have the path of the copied image file. So I convert the path string to path Uri and load it in the image cropper in the following way:I went through the following links to come up with this solution:
createImageFile()
from here)The text was updated successfully, but these errors were encountered: