为什么它没有通过Android中的意图打开已定义的文件夹? [英] Why it is not opening defined folder via intent in Android?

查看:102
本文介绍了为什么它没有通过Android中的意图打开已定义的文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码将重定向到具有打开的导航功能的驱动器,但未打开实际的给定路径

旧代码

Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/foldername/");
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setDataAndType(uri, "text/csv");
startActivity(intent);

我通过uri获取的路径值为:
/storage/emulated/0/myfolder

The path value I am getting through uri is:
/storage/emulated/0/myfolder

新代码

Intent resultIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
resultIntent.addCategory(Intent.CATEGORY_OPENABLE);
Uri uri = 
Uri.parse(Environment.getExternalStorageDirectory().getPath() + 
"/myfolder/");
resultIntent.setType("*/*");
resultIntent.putExtra("android.provider.extra.INITIAL_URI", uri);
// show the entire internal storage tree
//resultIntent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivity(resultIntent);

让我知道问题所在,此代码中的问题所在.

Let me know the issue where is the problem in this code.

推荐答案

private static final int READ_REQUEST_CODE = 10;
...
/**
 * Fires an intent to spin up the "file chooser" UI and select an image.
 */
public void performFileSearch() {

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType(uri, "text/*");

    startActivityForResult(intent, READ_REQUEST_CODE);
}

用户在选择器中选择文档后,onActivityResult() 被叫. resultData参数包含指向的URI 选定的文档.使用getData()提取URI.当你有 它,您可以使用它来检索用户想要的文档.

After the user selects a document in the picker, onActivityResult() gets called. The resultData parameter contains the URI that points to the selected document. Extract the URI using getData(). When you have it, you can use it to retrieve the document the user wants.

@Override
public void onActivityResult(int requestCode, int resultCode,
        Intent resultData) {

    if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {

        Uri uri = null;
        if (resultData != null) {
            uri = resultData.getData();
            Log.i(TAG, "Uri: " + uri.toString());
            showImage(uri);
        }
    }
}

这篇关于为什么它没有通过Android中的意图打开已定义的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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