0

I'm trying to provide sd card access inside my app with Storage Access Framework.

This is how I call an intent to let the user choose the sd card directory.

private void openDocumentTree() {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    intent.addFlags(
            Intent.FLAG_GRANT_READ_URI_PERMISSION
                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                    | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
                    | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
    startActivityForResult(intent, REQUEST_CODE_OPEN_DOCUMENT_TREE);
}

And this is how I manage the intent result to set permissions:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_OPEN_DOCUMENT_TREE:
                if (resultCode == Activity.RESULT_OK) {
                    Uri treeUri = data.getData();
                    int takeFlags = data.getFlags();
                    takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
                                  Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        this.getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
                    }
               }
        }

But still can't save any file on sd card.

Value of treeUri on my device is:

content://com.android.externalstorage.documents/tree/6921-B9FD%3A

What I missed here that system still couldn't let the user have access to sd card(saving a simple file on sd card)?

4

1 回答 1

0

好的。这就是我使用提供的权限在 sd 卡上复制图像的方式,该权限调用文件选择器以由用户选择 sd 卡目录(用户可能选择了错误的目录,因此请为它准备代码)。

private void newcopyFile(File fileInput, String outputParentPath,
                        String mimeType, String newFileName) {

    DocumentFile documentFileGoal = DocumentFile.fromTreeUri(this, treeUri);

    String[] parts = outputParentPath.split("\\/");
    for (int i = 3; i < parts.length; i++) { //ex: parts:{"", "storage", "extSdCard", "MyFolder", "MyFolder", "MyFolder"}
        if (documentFileGoal != null) {
            documentFileGoal = documentFileGoal.findFile(parts[i]);
        }
    }
    if (documentFileGoal == null) {
        Toast.makeText(MainActivity.this, "Directory not found", Toast.LENGTH_SHORT).show();
        return;
    }

    DocumentFile documentFileNewFile = documentFileGoal.createFile(mimeType, newFileName);

    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
        outputStream = getContentResolver().openOutputStream(documentFileNewFile.getUri());
        inputStream = new FileInputStream(fileInput);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        if (outputStream != null) {
            byte[] buffer = new byte[1024];
            int read;
            if (inputStream != null) {
                while ((read = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, read);
                }
            }
            if (inputStream != null) {
                inputStream.close();
            }
            inputStream = null;
            outputStream.flush();
            outputStream.close();
            outputStream = null;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

注意:阅读问题下的所有评论

并注意设置 a如果ContentObserverContentResolver想在新创建的图像在内部注册时收到通知ContentResolver,因此是时候查询它(如果您愿意)。

于 2016-07-25T14:15:49.373 回答