当前位置: 首页 > 知识库问答 >
问题:

从Storage Access Framework UI获取文件夹后保存图像

罗智刚
2023-03-14

我为用户设置了一个首选项,以选择保存文件夹为我的应用程序使用存储访问框架。获得urionActivityResult后,我将其作为字符串保存到SharedPreferences中,并在要保存时保存图像。

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();
}
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:无效uri:content://com.android.externalstorage.documents/tree/612e-b7bf%3aname/imagepfd.jpg在线pfd=getContentResolver().openfileDescriptor(uri,“w”);

这就是我如何调用这个方法,currentFolder是我使用intent和第一个方法得到的相同文件夹。

saveImageWithParcelFileDescriptor(currentFolder, "imagePFD");

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

共有1个答案

禄源
2023-03-14

如何检查文件夹是否存在

documentfile有一个exists()方法。理论上,pickeddir.exists()应该告诉您它是否存在。实际上,这取决于存储提供商。

我还想用这个方法使用ParcelFileDescriptor保存相同的图像

哪种方法更好

第一个。

为什么呢?

 类似资料:
  • 当我从图库中选择一个图像时,我可以获得该图像的Uri,如下所示: 上面检索到的Uri的格式是content://media/external/images/media/7266 但是,当我试图获取刚保存为文件的图像的Uri时,无法以这种格式检索Uri: 从上面的代码获得的Uri是/storage/emulated/0/dcim/camera/02-04-16 12-49-16.png 我相信,这不

  • 问题内容: 我需要一种从本地文件夹中获取所有图像的方法,以便在本地也可以运行演示文稿。由于这是不可能的,服务器将不会尝试从本地文件夹中获取图像。 我需要使用.js,因为它无法在本地PC上运行,因此无法使用.php(这样会更容易)。 说我需要从 学习中 获取所有图像 / 问题答案: 我认为您最好的选择是在Javascript中使用新的File API。是具有很多从文件系统读取文件的功能。 (代码从这

  • 本文向大家介绍从PHP文件夹中获取所有图像,包括了从PHP文件夹中获取所有图像的使用技巧和注意事项,需要的朋友参考一下 glob函数可用于从特定文件夹获取图像。以下是相同的示例代码- 指定图像文件夹的路径,并提取所有扩展名为.png的文件。它们在foreach循环的帮助下显示- 基于包含所有图像的文件夹,返回存在于文件夹内的每个图像的路径。

  • 问题内容: 我想使用“文件类”从项目文件夹中获取文件,我该怎么办? 问题答案: 嗯,有很多不同的方法来获取Java文件,但这是一般要点。 不要忘记,您至少需要将其包装在a 中,因为File是其中的一部分,这意味着它必须具有try-catch块。 不是要解决Ericson的问题,而是如果您使用的是实际的软件包,则除非明确使用文件的位置,否则文件的位置将有问题。相对路径与Packages混为一谈。 即

  • 我想通过使用“文件类”从项目文件夹中获取文件,我如何做到这一点?

  • 问题内容: 当我在eclipse中导出可执行jar文件时,也需要使用res文件夹进行编译,当我使用不起作用的方法时。 当前读取的图像代码 代码无效 问题答案: 我现在已经解决了问题-这是有效的代码 fileName的值只是一个图像名称,例如BufferedImage img = loadImage(“ background.png”); 感谢大家的帮助。