英文:
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:
<manifest ... >
<!-- This attribute is "false" by default on apps targeting
Android 10 or higher. -->
<application android:requestLegacyExternalStorage="true" ... >
...
</application>
</manifest>
hope this can help who has the same problem, thanks!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论