如何使用Intent.ACTION_VIEW查看文件夹的内容?

5

我正在尝试使用Intent查看特定文件夹中的图像。我的代码如下:

Intent intent = new Intent();  
intent.setAction(Intent.ACTION_VIEW);  
Uri imgUri = Uri.parse("file://sdcard/download");  
intent.setDataAndType(imgUri, "image/*");  
startActivity(intent);  

然而,每次运行时,我都会在日志中得到以下信息:

02-25 00:40:16.271: ERROR/(8359): 无法打开 '/download'
02-25 00:40:16.271: ERROR/(8359): 无法打开 '/download'

我做错了什么?


你有访问SD卡的权限吗? - Mudassir
这与问题完全无关,但是还有其他人没有看到此问题的编辑/重新标记链接吗?我错过了什么导致它们不在那里吗? - Andrew Marshall
Mudassir,我确实可以访问sd卡,我通过在其中创建一个简单的文本文件进行了测试。 - Jevarlo
你尝试过在URI中添加额外的斜杠吗?例如:"file:///sdcard/download"。 - Michael
我刚刚尝试了一下,进展有点了!但现在我遇到了另一个错误!在我的URI中添加一个额外的斜杠会导致这个错误:02-25 07:37:29.106: ERROR/(10632): Not JPEG: /sdcard/download/。即使在URI末尾添加通配符,使其看起来像这样“file:///sdcard/download/*”,也无法解决它。 - Jevarlo
通过该意图,它试图找到一个应用程序来打开文件“file://sdcard/download”。但这不是一个文件,而是一个文件夹。您需要一个不同的意图。 - RvdK
4个回答

1
尝试像这样做
Intent intent = new Intent();
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);

或者

Intent intent = new Intent();
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);

1

我认为这比其他人建议的要简单得多。我相信路径是区分大小写的。在你的例子中,“file://sdcard/Download”应该可以工作。


0
我之前遇到过类似的问题,需要让用户从预定位置选择照片。目前我正在使用的解决方案需要在设备上安装某种文件管理器。我使用的是名为“OI文件管理器”的应用,可以在Android/Play市场免费下载。当然,你也可以尝试使用其他的文件管理器。
 //--Note: You can supply the full location of the folder but if you don't know it and
//         if the folder location is on the sdcard, provide the following:
 File root = new File(Environment.getExternalStorageDirectory() + File.separator + "myFolder" + File.separator);
 Uri uri = Uri.fromFile(root);

 Intent intent = new Intent();
 intent.setAction(android.content.Intent.ACTION_VIEW);
 intent.setData(Uri.fromFile(root));
 startActivityForResult(intent, PIC_REQUEST);

这将会打开一个对话框,要求您选择一个选项来处理请求。当它这样做时,只需在选择“OI文件管理器”选项之前,在底部角落的框中勾选“默认使用...”。这将设置每次启动意图时自动打开您指定的位置,以便您可以查看内容,而无需每次启动意图时都钻到文件夹位置。依赖第三方应用程序并不好,但这可能是您的一个选择。


0
Intent intent = new Intent();  
intent.setAction(Intent.ACTION_GET_CONTENT);
Uri imgUri = Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
intent.setDataAndType(imgUri, "file/*");   
startActivity(intent);

对我来说有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接