Android存储访问框架:我应该使用哪个路径来保存文件?

时间:2017-04-12 15:07:03

标签: android save android-sdcard ioexception storage-access-framework

我通过SAF获得我的可移动SD卡的权限,代码如下。 点击“浮动操作”按钮"文件"我将权限授予我的应用。之后我想用FAB"文件夹"。

创建一个文件

当我尝试将文件保存到uri-path时,它表示该路径不存在。 我必须使用哪条路径? (String [] path仅用于测试不同的路径...)

FAB-文件

FloatingActionButton fab_new_file = (FloatingActionButton) rLayoutFrgEmpresas.findViewById(R.id.fab_menu_item_file);
fab_new_file.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        System.out.print("FAB File clicked");
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
        intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        startActivityForResult(intent, 42);
    }
});
mainPath = this.getArguments().getString("mainpath");
getFilesFromDir(mainPath, mainPath);
return rLayoutFrgEmpresas;

onActivityActionResult

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        System.out.print("onActivityResult started");
        if (resultCode == Activity.RESULT_OK) {
            if (data != null) {
                treeUri = data.getData();

                System.out.print("treeUri: "+treeUri);
                if (requestCode == 42){

                    int takeFlags = data.getFlags();
                    takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

                    getContext().getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
                    System.out.print("Permissions: "+getContext().getContentResolver().getPersistedUriPermissions());

                    editor = getActivity().getSharedPreferences("spicysoftware.com.phonemanager", MODE_PRIVATE).edit();
                    editor.putString("treeUri", treeUri.toString());
                    if(editor.commit()){
                        System.out.print("treeUri sharedPref added");
                    }
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

FAB-文件夹

FloatingActionButton fab_new_folder = (FloatingActionButton) rLayoutFrgEmpresas.findViewById(R.id.fab_menu_item_folder);
fab_new_folder.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Log.i("Information: ", "FAB Folder clicked");

        String str_treeUri = prefs.getString("treeUri", null);
        Uri myUri = Uri.parse(str_treeUri);

        Log.i("Information: ", "myUri from shared Pref: "+myUri);

        String[] path = {
                ""+myUri,
                ""+myUri+"/",
                "/com.android.externalstorage.documents/tree/B9BE-18A6/",
                "/tree/B9BE-18A6/",
                "/storage/B9BE-18A6/"
        };

        for(int i=0; i<path.length; i++) {
            File testfile = new File("" + path[i] + "testfile" + System.currentTimeMillis() + ".txt");
            if (!testfile.exists()) {
                Log.i("Information: ", testfile+ " does not exist");
                try {
                    testfile.createNewFile();
                    getFilesFromDir(textView_currentPath.getText() + "", textView_currentPath.getText() + "");
                    Log.i("File " + testfile, "has been created!");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
});

logcat的

*Before clicked FAB-File*
04-12 16:41:26.748 2197-2197/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processPointer 0
04-12 16:41:26.821 2197-2197/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processPointer 1
04-12 16:41:26.833 2197-2197/spicysoftware.com.phonemanager I/Information:: FAB File clicked
04-12 16:41:26.868 2197-2197/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0
04-12 16:41:27.036 2197-2197/spicysoftware.com.phonemanager D/InputTransport: Input channel destroyed: fd=73
04-12 16:41:34.131 2197-2197/spicysoftware.com.phonemanager I/Information:: onActivityResult started
04-12 16:41:34.140 2197-2197/spicysoftware.com.phonemanager I/Information:: Permissions: [UriPermission {uri=content://com.android.externalstorage.documents/tree/B9BE-18A6%3A, modeFlags=3, persistedTime=1492008094137}]
04-12 16:41:34.151 2197-2197/spicysoftware.com.phonemanager I/Information:: treeUri sharedPref added
04-12 16:41:34.173 2197-2285/spicysoftware.com.phonemanager D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1080x1920]-format:1
*After clicked FAB-File*

    *Before clicked FAB-Folder*
04-12 17:04:36.011 25159-25159/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processPointer 0
04-12 17:04:36.084 25159-25159/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processPointer 1
04-12 17:04:36.093 25159-25159/spicysoftware.com.phonemanager I/Information:: FAB Folder clicked
04-12 17:04:36.093 25159-25159/spicysoftware.com.phonemanager I/Information:: myUri from shared Pref: content://com.android.externalstorage.documents/tree/B9BE-18A6%3A
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager I/Information:: content:/com.android.externalstorage.documents/tree/B9BE-18A6%3Atestfile1492009476094.txt does not exist
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err: java.io.IOException: No such file or directory
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.File.createNewFile(File.java:948)
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at spicysoftware.com.phonemanager.StorageFragment$1.onClick(StorageFragment.java:172)
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View.performClick(View.java:6207)
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View$PerformClick.run(View.java:23639)
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Looper.loop(Looper.java:154)
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6688)
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager I/Information:: content:/com.android.externalstorage.documents/tree/B9BE-18A6%3A/testfile1492009476095.txt does not exist
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err: java.io.IOException: No such file or directory
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.File.createNewFile(File.java:948)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at spicysoftware.com.phonemanager.StorageFragment$1.onClick(StorageFragment.java:172)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View.performClick(View.java:6207)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View$PerformClick.run(View.java:23639)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Looper.loop(Looper.java:154)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6688)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager I/Information:: /com.android.externalstorage.documents/tree/B9BE-18A6/testfile1492009476095.txt does not exist
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err: java.io.IOException: No such file or directory
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.File.createNewFile(File.java:948)
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at spicysoftware.com.phonemanager.StorageFragment$1.onClick(StorageFragment.java:172)
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View.performClick(View.java:6207)
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View$PerformClick.run(View.java:23639)
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Looper.loop(Looper.java:154)
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6688)
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager I/Information:: /tree/B9BE-18A6/testfile1492009476096.txt does not exist
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err: java.io.IOException: No such file or directory
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.File.createNewFile(File.java:948)
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at spicysoftware.com.phonemanager.StorageFragment$1.onClick(StorageFragment.java:172)
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View.performClick(View.java:6207)
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View$PerformClick.run(View.java:23639)
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Looper.loop(Looper.java:154)
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6688)
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager I/Information:: /storage/B9BE-18A6/testfile1492009476097.txt does not exist
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err: java.io.IOException: Permission denied
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.File.createNewFile(File.java:948)
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at spicysoftware.com.phonemanager.StorageFragment$1.onClick(StorageFragment.java:172)
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View.performClick(View.java:6207)
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View$PerformClick.run(View.java:23639)
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Looper.loop(Looper.java:154)
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6688)
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
04-12 17:04:48.021 25159-25159/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processKey 0
04-12 17:04:48.046 25159-25159/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processKey 1
    *After clicked FAB-Folder*

1 个答案:

答案 0 :(得分:0)

  

我通过SAF获取我的可移动SD卡的权限,代码低于

不,你没有。您可以访问文档树。文档树所在的位置取决于用户。它可能位于外部存储设备,可移动存储设备,Google云端硬盘驱动器或用户拥有DocumentProvider的任何其他位置。

  

之后我想用FAB&#34;文件夹&#34;。

创建一个文件

您可以使用DocumentFilefromTreeUri()来包裹您从Uri返回的ACTION_OPEN_DOCUMENT_TREE。然后,使用它来创建子树,并获取指向这些树中的内容的Uri值。然后,使用ContentResolveropenOutputStream()Uri一起获得OutputStream,您可以在其中撰写内容。

  

我必须使用哪条路径?

无。 Uri不是文件。