I am trying to create a directory of my app in device Download folder.I am trying to use storage access framework to create directory in devices running on android q or above.The code is working fine in most of the devices and I am able to create directory and store file in it .But in some devices I am getting error.
This is the intent:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, CREATE_FOLDER);
This is the onActivityResult() where I am getting error:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CREATE_FOLDER:
Uri currentUri = null;
if (resultCode == RESULT_OK && data != null) {
Uri currentUri = data.getData();
if(currentUri==null){
Toast.makeText(this,
"Device does not support saf",
Toast.LENGTH_SHORT).show();
return; }
else{
final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContentResolver().takePersistableUriPermission(currentUri,takeFlags);
DocumentFile parentDocumentFile = DocumentFile.fromTreeUri(this, currentUri);
DocumentFile childDocumentFile = parentDocumentFile.createDirectory(getResources().getString(R.string.app_name));
}
}
// saving uri of folder created for future use
SharedPreferences.Editor folderUri = getSharedPreferences("folderUri", 0).edit();
folderUri.putString("folderUri",currentUri.toString());
folderUri.apply();
}
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
I have added read/write permission in android manifest and have also asked users for permission. I have asked some users on whose device app was crashing and they have granted all the permissions.So,I am not sure what the problem is and why it is happening in only few devices.
Below is the stacktrace from one of the crashes:
java.lang.RuntimeException:
at android.app.ActivityThread.deliverResults (ActivityThread.java:5096)
at android.app.ActivityThread.handleSendResult (ActivityThread.java:5137)
at android.app.servertransaction.ActivityResultItem.execute (ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2146)
at android.os.Handler.dispatchMessage (Handler.java:107)
at android.os.Looper.loop (Looper.java:237)
at android.app.ActivityThread.main (ActivityThread.java:7770)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1047)
Caused by: java.lang.SecurityException:
at android.os.Parcel.createException (Parcel.java:2088)
at android.os.Parcel.readException (Parcel.java:2056)
at android.os.Parcel.readException (Parcel.java:2004)
at android.app.IUriGrantsManager$Stub$Proxy.takePersistableUriPermission (IUriGrantsManager.java:280)
at android.content.ContentResolver.takePersistableUriPermission (ContentResolver.java:2435)
at package.SomeActivity.somemethod (SomeActivity.java:1340)
at package.SomeActivity.onRequestPermissionsResult (SomeActivity.java:1206)
at android.app.Activity.dispatchRequestPermissionsResult (Activity.java:8500)
at android.app.Activity.dispatchActivityResult (Activity.java:8322)
at android.app.ActivityThread.deliverResults (ActivityThread.java:5089)
at android.app.ActivityThread.handleSendResult (ActivityThread.java:5137)
at android.app.servertransaction.ActivityResultItem.execute (ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2146)
at android.os.Handler.dispatchMessage (Handler.java:107)
at android.os.Looper.loop (Looper.java:237)
at android.app.ActivityThread.main (ActivityThread.java:7770)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1047)
As we can see that the error report indicates that there is no permission ,but I am asking for permission and users are granting the permissions.
Below is another stack trace of same error but on different device.
Caused by: java.lang.NullPointerException:
at package.SomeActivity.b (SomeActivity.java:71)
at package.SomeActivity.onRequestPermissionsResult (SomeActivity.java:187)
at android.app.Activity.dispatchRequestPermissionsResult (Activity.java:8474)
at android.app.Activity.dispatchActivityResult (Activity.java:8296)
at android.app.ActivityThread.deliverResults (ActivityThread.java:5090)
at android.app.ActivityThread.handleSendResult (ActivityThread.java:5138)
at android.app.servertransaction.ActivityResultItem.execute (ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2147)
at android.os.Handler.dispatchMessage (Handler.java:107)
at android.os.Looper.loop (Looper.java:237)
at android.app.ActivityThread.main (ActivityThread.java:7814)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1068)
The difference is while in the first case the error was Security Exception in above case it is NullPointerException which points to this line in the codes: grantUriPermission(getPackageName(),currentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
So,I am not sure but may be onActivityResult is returning a null uri (Uri currentUri=data.getdata() is null) and the app does not have path or permission to create directory which is causing the app to crash.
So,I would like to know what am I doing wrong and why is it affecting only few devices (five cases four of them in Samsung devices).I would appreciate any help regarding this.Thanks in advance.