Reputation: 101
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)
Upvotes: 0
Views: 3234
Reputation: 4327
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
Upvotes: 1