从Storage Access Framework UI获取文件夹后保存图像 [英] Saving an image after getting folder from Storage Access Framework UI

查看:178
本文介绍了从Storage Access Framework UI获取文件夹后保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为用户设置了首选项,以使用Storage Access Framework为我的应用选择保存文件夹.在获取uri onActivityResult后,我将其保存为String并保存到SharedPreferences,并在要保存图像时保存图像.

I set a preference for users to select save folder for my app using Storage Access Framework. After getting uri onActivityResult I save it to SharedPreferences as String and save image when it's to be saved.

我正在使用此方法成功保存图像.

I'm using this method to save image successfully.

public void saveImageWithDocumentFile(String uriString, String mimeType, String name) {
    isImageSaved = false;
    try {
        Uri uri = Uri.parse(uriString);
        DocumentFile pickedDir = DocumentFile.fromTreeUri(this, uri);
        DocumentFile file = pickedDir.createFile(mimeType, name);
        OutputStream out = getContentResolver().openOutputStream(file.getUri());
        isImageSaved = mBitmap.compress(CompressFormat.JPEG, 100, out);
        out.close();

    } catch (IOException e) {
        throw new RuntimeException("Something went wrong : " + e.getMessage(), e);
    }
    Toast.makeText(MainActivity.this, "Image saved  " + isImageSaved, Toast.LENGTH_SHORT).show();
}

如果用户删除使用SAF选择的文件夹,则iget null DocumentFile file并导致应用程序崩溃.我的第一个问题是如何在不再次打开SAF ui的情况下检查文件夹是否存在.

If user deletes the folder selected using SAF, iget null DocumentFile file and app crashes. My first question is how can i check if folder exist without opening SAF ui again.

我也想使用ParcelFileDescriptor通过这种方法保存相同的图像

I also want to use ParcelFileDescriptor to save same image with this method

public void saveImageWithParcelFileDescriptor(String folder, String name) {
    if (mBitmap == null) {
        Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
        return;
    }
    String image = folder + File.separator + name + ".jpg";
    Toast.makeText(MainActivity.this, "image " + image, Toast.LENGTH_LONG).show();

    Uri uri = Uri.parse(image);
    ParcelFileDescriptor pfd = null;
    FileOutputStream fileOutputStream = null;
    try {
        pfd = getContentResolver().openFileDescriptor(uri, "w");
        fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
        mBitmap.compress(CompressFormat.JPEG, 100, fileOutputStream);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (pfd != null) {
            try {
                pfd.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    Toast.makeText(MainActivity.this, "Image saved  " + isImageSaved, Toast.LENGTH_SHORT).show();

}

我得到java.lang.IllegalArgumentException: Invalid URI: content://com.android.externalstorage.documents/tree/612E-B7BF%3Aname/imagePFD.jpg 在线pfd = getContentResolver().openFileDescriptor(uri, "w");

这就是我称为此方法的方式, currentFolder 是我使用intent时获得的文件夹,并且与第一种方法使用的文件夹相同.

This is how I call this method, currentFolder is the folder I get using intent and same folder on first method.

saveImageWithParcelFileDescriptor(currentFolder, "imagePFD");

如何解决此问题,哪种方法更可取,为什么?

How can I fix this, and which method is more preferable and why?

推荐答案

我如何检查文件夹是否存在

how can i check if folder exist

DocumentFile具有exists()方法.从理论上讲,pickedDir.exists()应该告诉您它是否存在.实际上,取决于存储提供商.

DocumentFile has an exists() method. In theory, pickedDir.exists() should tell you if it exists. In practice, it is up to the storage provider.

我也想使用ParcelFileDescriptor通过这种方法保存相同的图像

I also want to use ParcelFileDescriptor to save same image with this method

该代码存在错误:

  • 您无法使用grantUriPermission()

您不能通过串联发明任意的Uri值,特别是对于不是您自己的提供者

You cannot invent arbitrary Uri values via concatenation, particularly for a provider that is not yours

我该如何解决

How can I fix this

删除它.

哪种方法更可取

which method is more preferable

第一个.

为什么?

第一个可以工作,可能需要稍作调整.第二个没有工作的机会.

The first one will work, perhaps with minor adjustment. The second one has no chance of working.

这篇关于从Storage Access Framework UI获取文件夹后保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆