如何将图像写入共享位置以便于访问?

时间:2018-04-06 15:02:33

标签: java android

我正在处理的应用程序使用相机拍摄照片并对其进行处理。这使用以下代码:

private String getImageFolder() throws Exception {
    File mediaDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "my-app-folder");
    if (!mediaDir.exists()) {
        if (!mediaDir.mkdirs()) {
            throw new Exception("Cannot create or find the image directory at " + mediaDir.getAbsolutePath());
        }
    }
    return mediaDir.getPath();
}

我遇到的问题是它将它存储在/storage/0/simulated/android/data/com.myapp.codebase/Pictures/my-app-folder这样的路径上,这个应用程序的一个重要部分就是用户可以轻松地通过USB复制从中获取的图片,所以真的我需要它在/Pictures/my-app-folder这样的文件夹中 - 如果我说实话,我认为这就是getExternalFilesDir(Environment.DIRECTORY_PICTURES)会做的事情。我尝试使用Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),但当我尝试使用该路径时,我只给了FileNotFoundException

我在运行时启用并检查了以下存储权限:

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

为了将图像放在一致,易于访问的文件路径上,我需要做些什么?

1 个答案:

答案 0 :(得分:1)

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) but that just gave me a FileNotFoundException when I tried to use the path.

然后您忘记在清单文件中输入正确的权限。

今年你是#1245忘记要求运行时权限。

谷歌为他们。