2

I am experimenting with Storage Access Framework.

I am using NextCloud platform. This platform supports the ACTION_OPEN_DOCUMENT_TREE intent, that the GDrive app itself does not support yet.

My app lets the user select a cloud folder on the provider I decided to connect to NextCloud service.

static public void openPickerForFolderSelection(Activity activity, int requestCode) {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    intent.addFlags((Intent.FLAG_GRANT_PREFIX_URI_PERMISSION));
    activity.startActivityForResult(intent, requestCode);
}

then in onActivityResult()

if (resultCode == -1)
    if (requestCode == 0) {

       final Uri uri = takePermanentReadWritePermissions(activity, returnedIntent.getData(), returnedIntent.getFlags());
       Log.d("SAF", "uri=" + uri.toString());
        Log.d("SAF", "read/write permissions=" + arePermissionsGranted(activity, uri.toString()));
//permissions are OK


df=DocumentFile.fromTreeUri(activity,uri);
DocumentFile df2= df.createFile("text/plain","newfile.txt");
Log.d("created",df2.getName());
}

permissions are ok

static boolean arePermissionsGranted(Activity activity, String uriString) {

    Uri uri = Uri.parse(uriString);


    ContentResolver resolver = activity.getContentResolver();
    List<UriPermission> list = resolver.getPersistedUriPermissions();
    for (int i = 0; i < list.size(); i++) {
Log.d("SAF","checking permissions of "+list.get(i).getUri().toString());
        if (((Uri.decode(list.get(i).getUri().toString())).equals(Uri.decode(uriString))) && list.get(i).isWritePermission() && list.get(i).isReadPermission()) {
            return true;
        }


    }

    return false;
}

but I have a null df2 (null reference error from Log.d() instruction). In fact df2 is null because the following exception is issued before:

com.example.safevents W/DocumentsContract: Failed to create document
android.os.ParcelableException: java.io.FileNotFoundException: Failed to upload document with path /test/newfile.txt
    at android.os.ParcelableException$1.createFromParcel(ParcelableException.java:82)
    at android.os.ParcelableException$1.createFromParcel(ParcelableException.java:80)
    at android.os.Parcel.readParcelable(Parcel.java:2860)
    at android.os.Parcel.readException(Parcel.java:2008)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.call(ContentProviderNative.java:651)
    at android.content.ContentProviderClient.call(ContentProviderClient.java:483)
    at android.provider.DocumentsContract.createDocument(DocumentsContract.java:1180)
    at android.provider.DocumentsContract.createDocument(DocumentsContract.java:1162)
    at androidx.documentfile.provider.TreeDocumentFile.createFile(TreeDocumentFile.java:53)
    at androidx.documentfile.provider.TreeDocumentFile.createFile(TreeDocumentFile.java:45)
    at com.example.safevents.MainActivity.onActivityResult(MainActivity.java:107)
    at android.app.Activity.dispatchActivityResult(Activity.java:7295)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4361)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4409)
    at android.app.ActivityThread.-wrap19(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1670)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6687)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
 Caused by: java.io.FileNotFoundException: Failed to upload document with path /test/newfile.txt
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
    at android.os.ParcelableException.readFromParcel(ParcelableException.java:56)
    at android.os.ParcelableException$1.createFromParcel(ParcelableException.java:82) 
    at android.os.ParcelableException$1.createFromParcel(ParcelableException.java:80) 
    at android.os.Parcel.readParcelable(Parcel.java:2860) 
    at android.os.Parcel.readException(Parcel.java:2008) 
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) 
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) 
    at android.content.ContentProviderProxy.call(ContentProviderNative.java:651) 
    at android.content.ContentProviderClient.call(ContentProviderClient.java:483) 
    at android.provider.DocumentsContract.createDocument(DocumentsContract.java:1180) 
    at android.provider.DocumentsContract.createDocument(DocumentsContract.java:1162) 
    at androidx.documentfile.provider.TreeDocumentFile.createFile(TreeDocumentFile.java:53) 
    at androidx.documentfile.provider.TreeDocumentFile.createFile(TreeDocumentFile.java:45) 
    at com.example.safevents.MainActivity.onActivityResult(MainActivity.java:107) 
    at android.app.Activity.dispatchActivityResult(Activity.java:7295) 
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4361) 
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4409) 
    at android.app.ActivityThread.-wrap19(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1670) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6687) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810) 

What can I do now?

P5music
  • 3,197
  • 2
  • 32
  • 81
  • `intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); intent.addFlags((Intent.FLAG_GRANT_PREFIX_URI_PERMISSION));` That makes no sense. You cannot grant anything yourself there. You are the client. You should wait until the provider grants them. Then you can take them permanent in onActivityResult. Better remove those lines. – blackapps Dec 18 '19 at 13:49
  • You could check in onActivityResult if you get a WRITE permission. Probably there is only a READ permission. – blackapps Dec 18 '19 at 13:50
  • You are not checking df2 for null. – blackapps Dec 18 '19 at 13:52
  • @blackapps arePermissionsGranted() is checking write and read permissions, both permissions are granted. df2 is null because I get a null reference error. I edit the question with these information. – P5music Dec 18 '19 at 13:57
  • You updated your post but did not remove the flags. – blackapps Dec 18 '19 at 14:03
  • @blackapps Here you are. I edited the question. It's the same also without those flags. – P5music Dec 18 '19 at 14:10
  • `It's the same also without those flags.` Thats what i told you. Almost done. Why did you leave two? – blackapps Dec 18 '19 at 14:11
  • @blackapps I always put all those flags because the provider could act differently on their presence basis. In my opinion the most general and comprehensive code form is with all flags. I think I should edit the question back. – P5music Dec 18 '19 at 14:18
  • I have seen that before indeed that you always use them. Wrong. What makes you think a provider would look at those flags? Wrong. And you confuse others. Please post clean code only. – blackapps Dec 18 '19 at 14:19
  • @blackapps I explained the reason. You think all providers behave like the filesystem SAF DocumentProvider. You are wrong. So the most general code has been restored in my question. Those flags would have not been created if they were useless. – P5music Dec 18 '19 at 14:23
  • Please tell which provider uses them? – blackapps Dec 18 '19 at 14:24

0 Answers0