So here is something that I am just having a world of trouble understanding the issue with. Let me start by saying that I have the proper permissions settings in the manifest file. I also verify that the user has permission to access files in external storage. So here is the crazy issue. I am using UCrop to make it possible to crop an image in my app. For some reason, I have to copy the file to internal storage before I can use it with UCrop. Here is what I mean
private void LoadImage( Intent data ) {
try {
Uri imageUri = data.getData();
Uri tempUri = IO.CopyTemporaryBitmap( getApplicationContext(), imageUri );
StartCrop( tempUri , getFilesDir() + "/tempimage.png" );
}
catch ( Exception e) {
e.printStackTrace();
Toast.makeText(this, "Unable to load picture: ", Toast.LENGTH_LONG).show();
}
}
IO.CopyTemporaryBitmap
public static Uri CopyTemporaryBitmap( Context context, Uri src ) {
Bitmap bmp;
try {
InputStream imageStream = context.getContentResolver().openInputStream( src );
bmp = BitmapFactory.decodeStream(imageStream);
}
catch ( Exception e ) {
e.printStackTrace();
return null;
}
if ( bmp == null ) return null;
File tempPath = new File( GetTempFolder() );
File file = new File( GetTempFolder() + "tempImage.png" );
try {
boolean b = true;
if (!tempPath.exists()) b = tempPath.mkdirs();
if ( !file.exists() && b ) b = file.createNewFile();
if (b) {
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
}
return Uri.fromFile( file );
}
catch ( Exception e ) {
e.printStackTrace();
}
return null;
}
If I do that, it works. I have Read/Write access so I am able to copy the image to internal and then crop it but if I just do
private void LoadImage( Intent data ) {
try {
Uri imageUri = data.getData();
StartCrop( imageUri, getFilesDir() + "/tempimage.png" );
}
catch ( Exception e) {
e.printStackTrace();
Toast.makeText(this, "Unable to load picture: ", Toast.LENGTH_LONG).show();
}
}
Then I get
E/TransformImageView: onFailure: setImageUri
java.io.FileNotFoundException: open failed: EACCES (Permission denied)
at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:315)
at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:220)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1498)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:1338)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:1286)
at com.yalantis.ucrop.task.BitmapLoadTask.doInBackground(BitmapLoadTask.java:97)
at com.yalantis.ucrop.task.BitmapLoadTask.doInBackground(BitmapLoadTask.java:41)
at android.os.AsyncTask$3.call(AsyncTask.java:378)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
After quite a lot of debugging I found the culprit to be the setImageUri method in TransformImageView.java class which is part of UCrop and I did not write.
/**
* This method takes an Uri as a parameter, then calls method to decode it into Bitmap with specified size.
*
* @param imageUri - image Uri
* @throws Exception - can throw exception if having problems with decoding Uri or OOM.
*/
public void setImageUri(@NonNull Uri imageUri, @Nullable Uri outputUri) throws Exception {
int maxBitmapSize = getMaxBitmapSize();
BitmapLoadUtils.decodeBitmapInBackground(getContext(), imageUri, outputUri, maxBitmapSize, maxBitmapSize,
new BitmapLoadCallback() {
@Override
public void onBitmapLoaded(@NonNull Bitmap bitmap, @NonNull ExifInfo exifInfo, @NonNull String imageInputPath, @Nullable String imageOutputPath) {
mImageInputPath = imageInputPath;
mImageOutputPath = imageOutputPath;
mExifInfo = exifInfo;
mBitmapDecoded = true;
setImageBitmap(bitmap);
}
@Override
public void onFailure(@NonNull Exception bitmapWorkerException) {
Log.e(TAG, "onFailure: setImageUri", bitmapWorkerException);
if (mTransformImageListener != null) {
mTransformImageListener.onLoadFailure(bitmapWorkerException);
}
}
});
}
For whatever reason, "BitmapLoadUtils.decodeBitmapInBackground..." always triggers the "onFailure" option with "Permission Denied." Any clues?
Here is what I found that fixed the issue:
I added
android:requestLegacyExternalStorage="true"
to my manifest under the application section.
It looks like things have changed with storage. For details you can read,
https://commonsware.com/blog/2019/06/07/death-external-storage-end-saga.html