如何使用Uri打开特定目录? [英] How to open a particular directory using Uri?

查看:158
本文介绍了如何使用Uri打开特定目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个特定目录(在文件浏览器应用程序中),其中存储了由我的应用程序保存的图像。我可以通过 Uri.parse(imagesDir.getAbsolutePath())来获取该目录的Uri。我尝试了 this 以及其他,但是它什么也没做。这是我的代码到目前为止的样子:

I want to open a particular directory (in a file explorer app) where images saved by my app are stored. I can get the Uri of that directory by Uri.parse(imagesDir.getAbsolutePath()). I tried this, this and others but it just does nothing. This is how my code looks as of now:

Uri selectedUri = Uri.parse(imagesDir.getAbsolutePath());
int OPEN_REQUEST = 1337;
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setDataAndType(selectedUri, DocumentsContract.Document.MIME_TYPE_DIR);
if (intent.resolveActivityInfo(getPackageManager(), 0) != null) {
    startActivityForResult(intent, OPEN_REQUEST);
} else {
    Log.e("MainActivity", "Could not launch intent");
}

PS: imagesDir.getAbsolutePath() = / storage / emulated / 0 / Draw Easy

P.S: The value of imagesDir.getAbsolutePath() = /storage/emulated/0/Draw Easy

推荐答案


我想打开一个

I want to open a particular directory where images saved by my app are stored

Android从未真正支持此目录。

Android has never really supported this.


我可以通过Uri.parse(imagesDir.getAbsolutePath())获取该目录的Uri。

I can get the Uri of that directory by Uri.parse(imagesDir.getAbsolutePath()).

那是无效的 Uri 。充其量,请使用 Uri.fromFile(imagesDir)

That is an invalid Uri. At best, use Uri.fromFile(imagesDir).


这是我的代码的样子现在

This is how my code looks as of now

ACTION_OPEN_DOCUMENT 在数据列表中不包含 Uri 方面

与您想要的内容最接近的是 EXTRA_INITIAL_URI 添加到意图。但是,此文档仅可用于您以前从 ACTION_OPEN_DOCUMENT 获得的 Uri ACTION_OPEN_DOCUMENT_TREE 。在其他地方,例如从 File

The closest thing to what you want is to add EXTRA_INITIAL_URI to the Intent. However, this is only documented to work with a Uri that you previously obtained from ACTION_OPEN_DOCUMENT or ACTION_OPEN_DOCUMENT_TREE. It is unlikely to work with a Uri from some place else, such as from some File.

这篇关于如何使用Uri打开特定目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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