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

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

java.io.IOException: Permission denied for createNewFile()

问题

以下是翻译好的部分:

清单文件

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

Java 代码

  1. final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
  2. final String fileName = activity.getString(R.string.app_name) + (new Date().getTime()) + ".pdf";
  3. File file = new File(path, fileName);
  4. System.out.println("正在创建文件,文件名为 - " + fileName);
  5. try {
  6. checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, 101);
  7. file.createNewFile();
  8. System.out.println("成功创建文件,文件名为 - " + fileName);
  9. }
  10. catch (Exception e) {
  11. Log.e("TAG", "无法打开 ParcelFileDescriptor", e);
  12. }
  1. public void checkPermission(String permission, int requestCode)
  2. {
  3. if (ContextCompat.checkSelfPermission(this.activity, permission) == PackageManager.PERMISSION_DENIED) {
  4. ActivityCompat.requestPermissions(this.activity, new String[] { permission }, requestCode);
  5. }
  6. else {
  7. Toast.makeText(this.activity, "权限已经被授予", Toast.LENGTH_SHORT).show();
  8. }
  9. }

错误追踪

  1. java.io.IOException: 权限被拒绝
  2. at java.io.UnixFileSystem.createFileExclusively0(Native Method)
  3. at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
  4. at java.io.File.createNewFile(File.java:1008)
  5. at in.justnow.aryan.JsInterface$6.run(JsInterface.java:636)
  6. at android.os.Handler.handleCallback(Handler.java:883)
  7. at android.os.Handler.dispatchMessage(Handler.java:100)
  8. at android.os.Looper.loop(Looper.java:214)
  9. at android.app.ActivityThread.main(ActivityThread.java:7397)
  10. at java.lang.reflect.Method.invoke(Native Method)
  11. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
  12. 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

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

Java Code

  1. final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
  2. final String fileName = activity.getString(R.string.app_name) + (new Date().getTime()) + &quot;.pdf&quot;;
  3. File file = new File(path, fileName);
  4. System.out.println(&quot;Creating file with name - &quot;+fileName);
  5. try {
  6. checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, 101);
  7. file.createNewFile();
  8. System.out.println(&quot;Successfully created file with name - &quot;+fileName);
  9. }
  10. catch (Exception e) {
  11. Log.e(&quot;TAG&quot;, &quot;Failed to open ParcelFileDescriptor&quot;, e);
  12. }
  1. public void checkPermission(String permission, int requestCode)
  2. {
  3. if (ContextCompat.checkSelfPermission(this.activity, permission) == PackageManager.PERMISSION_DENIED) {
  4. ActivityCompat.requestPermissions( this.activity, new String[] { permission }, requestCode);
  5. }
  6. else {
  7. Toast.makeText(this.activity, &quot;Permission already granted&quot;, Toast.LENGTH_SHORT).show();
  8. }
  9. }

Error Trace

  1. java.io.IOException: Permission denied
  2. at java.io.UnixFileSystem.createFileExclusively0(Native Method)
  3. at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
  4. at java.io.File.createNewFile(File.java:1008)
  5. at in.justnow.aryan.JsInterface$6.run(JsInterface.java:636)
  6. at android.os.Handler.handleCallback(Handler.java:883)
  7. at android.os.Handler.dispatchMessage(Handler.java:100)
  8. at android.os.Looper.loop(Looper.java:214)
  9. at android.app.ActivityThread.main(ActivityThread.java:7397)
  10. at java.lang.reflect.Method.invoke(Native Method)
  11. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
  12. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)

答案1

得分: 1

你可以使用以下代码来创建你的PDF文件:

  1. final String fileName = getString(R.string.app_name) + (new Date().getTime()) + ".pdf";
  2. if (Build.VERSION.SDK_INT >= 29) {
  3. String filePath = PathUtils.getExternalAppDownloadPath().concat("/").concat(fileName);
  4. boolean isSuccess = FileUtils.createOrExistsFile(filePath);
  5. if (isSuccess) System.out.println("Successfully created file with name - " + fileName);
  6. else System.out.println("Something went wrong!");
  7. } else {
  8. String filePath = PathUtils.getExternalDownloadsPath().concat("/").concat(fileName);
  9. boolean isSuccess = FileUtils.createOrExistsFile(filePath);
  10. if (isSuccess) System.out.println("Successfully created file with name - " + fileName);
  11. else System.out.println("Something went wrong!");
  12. }

所使用的库:implementation 'com.blankj:utilcodex:1.29.0'

或者

不推荐使用)在你的 AndroidManifest.XML 文件中的 Application 标签内添加:android:requestLegacyExternalStorage="true"

英文:

You can create your PDF file using the following code :

  1. final String fileName = getString(R.string.app_name) + (new Date().getTime()) + &quot;.pdf&quot;;
  2. if (Build.VERSION.SDK_INT &gt;= 29) {
  3. String filePath = PathUtils.getExternalAppDownloadPath().concat(&quot;/&quot;).concat(fileName);
  4. boolean isSuccess = FileUtils.createOrExistsFile(filePath);
  5. if (isSuccess) System.out.println(&quot;Successfully created file with name - &quot; + fileName);
  6. else System.out.println(&quot;Something went wrong!&quot;);
  7. } else {
  8. String filePath = PathUtils.getExternalDownloadsPath().concat(&quot;/&quot;).concat(fileName);
  9. boolean isSuccess = FileUtils.createOrExistsFile(filePath);
  10. if (isSuccess) System.out.println(&quot;Successfully created file with name - &quot; + fileName);
  11. else System.out.println(&quot;Something went wrong!&quot;);
  12. }

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:

确定