英文:
java.io.IOException: Permission denied for createNewFile()
问题
以下是翻译好的部分:
清单文件
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Java 代码
final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
final String fileName = activity.getString(R.string.app_name) + (new Date().getTime()) + ".pdf";
File file = new File(path, fileName);
System.out.println("正在创建文件,文件名为 - " + fileName);
try {
   checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, 101);
   file.createNewFile();
   System.out.println("成功创建文件,文件名为 - " + fileName);
} 
catch (Exception e) {
   Log.e("TAG", "无法打开 ParcelFileDescriptor", e);
}
public void checkPermission(String permission, int requestCode)
{
    if (ContextCompat.checkSelfPermission(this.activity, permission) == PackageManager.PERMISSION_DENIED) {
        ActivityCompat.requestPermissions(this.activity, new String[] { permission }, requestCode);
    }
    else {
        Toast.makeText(this.activity, "权限已经被授予", Toast.LENGTH_SHORT).show();
    }
}
错误追踪
java.io.IOException: 权限被拒绝
        at java.io.UnixFileSystem.createFileExclusively0(Native Method)
        at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
        at java.io.File.createNewFile(File.java:1008)
        at in.justnow.aryan.JsInterface$6.run(JsInterface.java:636)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7397)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
英文:
I am trying to create a file in the external storage. I have added the permission in manifest file and also added checkPermission function before calling createNewFile() function. But I am still getting Permission Denied error.
Manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>     
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Java Code
final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
final String fileName = activity.getString(R.string.app_name) + (new Date().getTime()) + ".pdf";
File file = new File(path, fileName);
System.out.println("Creating file with name - "+fileName);
try {
   checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, 101);
   file.createNewFile();
   System.out.println("Successfully created file with name - "+fileName);
} 
catch (Exception e) {
   Log.e("TAG", "Failed to open ParcelFileDescriptor", e);
}
public  void checkPermission(String permission, int requestCode)
{
    if (ContextCompat.checkSelfPermission(this.activity, permission) == PackageManager.PERMISSION_DENIED) {
        ActivityCompat.requestPermissions( this.activity, new String[] { permission }, requestCode);
    }
    else {
        Toast.makeText(this.activity, "Permission already granted", Toast.LENGTH_SHORT).show();
    }
}
Error Trace
java.io.IOException: Permission denied
        at java.io.UnixFileSystem.createFileExclusively0(Native Method)
        at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
        at java.io.File.createNewFile(File.java:1008)
        at in.justnow.aryan.JsInterface$6.run(JsInterface.java:636)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7397)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
答案1
得分: 1
你可以使用以下代码来创建你的PDF文件:
final String fileName = getString(R.string.app_name) + (new Date().getTime()) + ".pdf";
if (Build.VERSION.SDK_INT >= 29) {
    String filePath = PathUtils.getExternalAppDownloadPath().concat("/").concat(fileName);
    boolean isSuccess = FileUtils.createOrExistsFile(filePath);
    if (isSuccess) System.out.println("Successfully created file with name - " + fileName);
    else System.out.println("Something went wrong!");
} else {
    String filePath = PathUtils.getExternalDownloadsPath().concat("/").concat(fileName);
    boolean isSuccess = FileUtils.createOrExistsFile(filePath);
    if (isSuccess) System.out.println("Successfully created file with name - " + fileName);
    else System.out.println("Something went wrong!");
}
所使用的库:implementation 'com.blankj:utilcodex:1.29.0'
或者
(不推荐使用)在你的 AndroidManifest.XML 文件中的 Application 标签内添加:android:requestLegacyExternalStorage="true"。
英文:
You can create your PDF file using the following code :
  final String fileName = getString(R.string.app_name) + (new Date().getTime()) + ".pdf";
        if (Build.VERSION.SDK_INT >= 29) {
            String filePath = PathUtils.getExternalAppDownloadPath().concat("/").concat(fileName);
            boolean isSuccess = FileUtils.createOrExistsFile(filePath);
            if (isSuccess) System.out.println("Successfully created file with name - " + fileName);
            else System.out.println("Something went wrong!");
        } else {
            String filePath = PathUtils.getExternalDownloadsPath().concat("/").concat(fileName);
            boolean isSuccess = FileUtils.createOrExistsFile(filePath);
            if (isSuccess) System.out.println("Successfully created file with name - " + fileName);
            else System.out.println("Something went wrong!");
        }
The used library : implementation 'com.blankj:utilcodex:1.29.0'
OR
(Not recommended) use :  android:requestLegacyExternalStorage="true" inside the Application Tag in your AndroidManifest.XML
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论