这里有一件事让我很难理解。首先,我在清单文件中设置了适当的权限。我还验证了用户是否具有访问外部存储文件的权限。但问题在于,我使用UCrop在应用程序中裁剪图像时,必须将文件复制到内部存储中才能使用它。我是这个意思。
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;
}
如果我这样做,它就起作用了。我有读写权限,所以我可以将图片复制到内部,然后裁剪它,但如果我只是这样做
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();
}
}
接着我得到了...
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)
经过大量的调试,我发现罪魁祸首是TransformImageView.java类中的setImageUri方法,该方法是UCrop的一部分,而我没有编写该方法。
/**
* 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);
}
}
});
}
无论出于何种原因,“BitmapLoadUtils.decodeBitmapInBackground...”始终触发“onFailure”选项,并显示“权限被拒绝”。有什么线索吗?