如何使用Uri打开特定目录? [英] How to open a particular directory using 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()$ c的值$ c> = / 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屋!