意图 ACTION_OPEN_DOCUMENT_TREE 似乎没有返回驱动的真实路径 [英] Intent ACTION_OPEN_DOCUMENT_TREE doesn't seem to return a real path to drive

查看:57
本文介绍了意图 ACTION_OPEN_DOCUMENT_TREE 似乎没有返回驱动的真实路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从连接到我的 Google Pixel 的 USB 存储设备读取文件.我目前正在使用这种方法来选择驱动器的路径,以便我可以查询其内容

I'm trying to read files from a USB storage device connected to my Google Pixel. I'm currently using this method to select the path of the drive so I can query it for its contents

private static final String TAG = "MainActivity";
private static final int REQUEST_CHOOSE_DRIVE = 1;
private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (TextView) findViewById(R.id.text);

    Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);

    startActivityForResult(i, REQUEST_CHOOSE_DRIVE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CHOOSE_DRIVE) {

        Uri uri = data.getData();

    }
}

但是,Uri 看起来像 /tree/...,它似乎不是 Android 文件系统中的真实路径(通过 adb shell 验证)).如何使用这个 uri 查询便携式存储设备的内容?我尝试使用给出的答案 here 但链接的函数返回 null.

However, the Uri looks something like /tree/... which doesn't seem to be a real path in the Android file system (verified via adb shell). How can I use this uri to query the contents of a portable storage device? I tried using the answer given here but the linked function returns null.

推荐答案

您正在获取树 Uri.因此,您需要添加以下代码以从 Tree Uri 获取文件.

You are getting tree Uri. So you need to Add below code to get files from Tree Uri.

        DocumentFile documentFile = DocumentFile.fromTreeUri(this, uri);
        for (DocumentFile file : documentFile.listFiles()) {

            if(file.isDirectory()){ // if it is sub directory
                // Do stuff with sub directory
            }else{
                // Do stuff with normal file
            }

           Log.d("Uri->",file.getUri() + "
");

        }

查询内容,可以使用下面的代码.

For the query the contents, you can use below code.

ContentResolver contentResolver = getActivity().getContentResolver();
    Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri,
            DocumentsContract.getTreeDocumentId(uri));
    Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri,
            DocumentsContract.getTreeDocumentId(uri));
Cursor docCursor = contentResolver.query(docUri, new String[]{
            Document.COLUMN_DISPLAY_NAME, Document.COLUMN_MIME_TYPE}, null, null, null);
    try {
        while (docCursor.moveToNext()) {
            Log.d(TAG, "found doc =" + docCursor.getString(0) + ", mime=" + docCursor
                    .getString(1));

        }
    } finally {
        // close cursor
    }

您可以查看 Google 示例代码:https://github.com/googlesamples/android-DirectorySelection/blob/master/Application/src/main/java/com/example/android/directoryselection/DirectorySelectionFragment.java#L150

You can check Google sample code: https://github.com/googlesamples/android-DirectorySelection/blob/master/Application/src/main/java/com/example/android/directoryselection/DirectorySelectionFragment.java#L150

这篇关于意图 ACTION_OPEN_DOCUMENT_TREE 似乎没有返回驱动的真实路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆