How can I use Intent.ACTION_VIEW to view the contents of a folder?

孤街浪徒 提交于 2019-12-18 13:32:22

问题


I'm trying to view the images in a specific folder using an intent. My code is as follows:

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

However, each time it runs, I get this message in the log:

02-25 00:40:16.271: ERROR/(8359): can't open '/download'
02-25 00:40:16.271: ERROR/(8359): can't open '/download'

What am I doing wrong?


回答1:


I think this is far simpler than everyone else is suggesting. I believe the path is case sensitive. In your example "file://sdcard/Download" should work.




回答2:


try something like this

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

or

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



回答3:


I had a similar problem a while back where I had an instance that required me to give the user the option to select a photo from a predetermined location. The working solution for this that I am currently using requires that you have some kind of file manger already on the device. The one that I am using is called "OI File Manager". It's free to download from the Android/Play market. With that being said you could try using:

 //--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);

What this will do is open a dialog asking for you to select an option to use to process the request. When it does that just check the box in the bottom corner "Use by default..." before selecting the "OI File Manager" option. This will set it so that each time the intent is launched it will automatically open your specified location so that you can view the contents without having to drill down to the folder location every time when the intent is launched. It's not good to have to rely on a third party app but it could be a option for you.




回答4:


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);

works for me..



来源:https://stackoverflow.com/questions/5114408/how-can-i-use-intent-action-view-to-view-the-contents-of-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!