2

所以这是我在理解这个问题时遇到的麻烦。首先让我说我在清单文件中有正确的权限设置。我还验证用户是否有权访问外部存储中的文件。所以这是一个疯狂的问题。我使用 UCrop 可以在我的应用程序中裁剪图像。出于某种原因,我必须先将文件复制到内部存储,然后才能将其与 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...”总是触发带有“Permission Denied”的“onFailure”选项。有什么线索吗?

4

2 回答 2

4

这是我发现解决问题的方法:

我添加了

android:requestLegacyExternalStorage="true"

到我的应用程序部分下的清单。

看起来事情已经随着存储而改变。有关详细信息,您可以阅读,

https://commonsware.com/blog/2019/06/07/death-external-storage-end-saga.html

于 2019-12-14T16:29:12.423 回答
0

好吧,这似乎是一个 API 级别问题,目标 API LEVEL 29 由于所谓的“范围机制”的问题,特别是在 Android 10 等新 Android 版本中

您首先提供的解决方案将是目前最好的。

于 2019-10-14T08:15:43.817 回答