Storage Access Framework-无法在使用ACTION_OPEN_DOCUMENT_TREE(NextCloud)选择的云文件夹中创建文件

时间:2019-12-18 13:04:38

标签: android file parcelable storage-access-framework nextcloud

我正在尝试使用Storage Access Framework。

我正在使用NextCloud平台。该平台支持ACTION_OPEN_DOCUMENT_TREE意图,GDrive应用程序本身尚不支持。

我的应用程序允许用户在我决定连接到NextCloud服务的提供商上选择一个云文件夹。

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);
}

然后在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());
}

权限还可以

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;
}

但是我有一个空df2(来自Log.d()指令的空引用错误)。 实际上df2为null,因为之前已发出以下异常:

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) 

我现在该怎么办?

0 个答案:

没有答案