java.io.IOException: 创建新文件时权限被拒绝()

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

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

&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;/&gt;     
&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot;/&gt;

Java Code

final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
final String fileName = activity.getString(R.string.app_name) + (new Date().getTime()) + &quot;.pdf&quot;;
File file = new File(path, fileName);
System.out.println(&quot;Creating file with name - &quot;+fileName);
try {
   checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, 101);
   file.createNewFile();
   System.out.println(&quot;Successfully created file with name - &quot;+fileName);
} 
catch (Exception e) {
   Log.e(&quot;TAG&quot;, &quot;Failed to open ParcelFileDescriptor&quot;, 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, &quot;Permission already granted&quot;, 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()) + &quot;.pdf&quot;;
        if (Build.VERSION.SDK_INT &gt;= 29) {
            String filePath = PathUtils.getExternalAppDownloadPath().concat(&quot;/&quot;).concat(fileName);
            boolean isSuccess = FileUtils.createOrExistsFile(filePath);
            if (isSuccess) System.out.println(&quot;Successfully created file with name - &quot; + fileName);
            else System.out.println(&quot;Something went wrong!&quot;);
        } else {
            String filePath = PathUtils.getExternalDownloadsPath().concat(&quot;/&quot;).concat(fileName);
            boolean isSuccess = FileUtils.createOrExistsFile(filePath);
            if (isSuccess) System.out.println(&quot;Successfully created file with name - &quot; + fileName);
            else System.out.println(&quot;Something went wrong!&quot;);
        }

The used library : implementation &#39;com.blankj:utilcodex:1.29.0&#39;

OR

(Not recommended) use : android:requestLegacyExternalStorage=&quot;true&quot; inside the Application Tag in your AndroidManifest.XML

huangapple
  • 本文由 发表于 2020年9月22日 02:57:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63998411.html
匿名

发表评论

匿名网友

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

确定