`File.createTempFile` 抛出权限被拒绝异常。

huangapple go评论89阅读模式
英文:

File.createTempFile throws Permission denied

问题

我想创建一个用于保存jpg格式图像的文件。

public static File createImageFile() throws IOException {
    final String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    final String imageFileName = "IMG_" + timestamp;
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME);
    storageDir.mkdirs();
    return File.createTempFile(imageFileName, ".jpg", storageDir);
}

上述是我用于创建文件的函数,位于名为FileHelper的类中。以下是我在使用该函数时的示例代码:

if (filePath == null) {
    File photoFile = null;
    try {
        photoFile = FileHelper.createImageFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    filePath = photoFile.getAbsolutePath();

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    try {
        bmp = getBitmap(filePath);
    } catch (Exception e) {
        e.printStackTrace();
        bmp = BitmapHelper.reduceResolution(filePath, widthPixels, heightPixels);
    }
}

它在一些设备上工作正常,但在一些设备上会抛出错误,错误信息如下:

W/System.err: java.io.IOException: Permission denied
W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
    at java.io.File.createTempFile(File.java:2018)
    at com.mogawe.mosurvei.util.FileHelper.createImageFile(FileHelper.java:671)

有人知道如何解决这个问题吗?

英文:

i want to make file for saving image with jpg format

 public static File createImageFile() throws IOException {
    final String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    final String imageFileName = "IMG_" + timestamp;
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME);
    storageDir.mkdirs();
    return File.createTempFile(imageFileName, ".jpg", storageDir);
}

above is my function to create file and located in a class named FileHelper, and this is example code when im using that function

 if (filePath == null) {
     File photoFile = null;
     try {
        photoFile = FileHelper.createImageFile();
     } catch (IOException e) {
        e.printStackTrace();
     }
     filePath = photoFile.getAbsolutePath();

     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inSampleSize = 2;
     try {
        bmp = getBitmap(filePath);
     } catch (Exception e) {
        e.printStackTrace();
        bmp = BitmapHelper.reduceResolution(filePath, widthPixels, heightPixels);
     }
  } 

it works in some devices and it also throws error in some of devices, and the error is:

W/System.err: java.io.IOException: Permission denied
W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
    at java.io.File.createTempFile(File.java:2018)
    at com.mogawe.mosurvei.util.FileHelper.createImageFile(FileHelper.java:671)

is there anyone know how to fix this?

答案1

得分: 0

我已经为自己的问题找到了解决方案>_<,所以解决方案在这里。如果您的目标是 Android 10(API 级别29)或更高版本,请在您的应用程序清单文件中将 requestLegacyExternalStorage 的值设置为 true

<manifest ... >
<!-- 默认情况下,针对 Android 10 或更高版本的应用程序此属性为“false”。 -->
  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>
</manifest>

希望这能帮助到有同样问题的人,谢谢!

英文:

I ve found solution for my own problem >_<, So the solution was here. If you target Android 10 (API level 29) or higher, set the value of requestLegacyExternalStorage to true in your app's manifest file:

&lt;manifest ... &gt;
&lt;!-- This attribute is &quot;false&quot; by default on apps targeting
     Android 10 or higher. --&gt;
  &lt;application android:requestLegacyExternalStorage=&quot;true&quot; ... &gt;
    ...
  &lt;/application&gt;
&lt;/manifest&gt;

hope this can help who has the same problem, thanks!

huangapple
  • 本文由 发表于 2020年8月27日 13:08:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63609556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定