简体   繁体   中英

How to open DCIM/Camera folder in default Files app from intent

I am making a camera app which takes both front camera and back camera images/video and I do not want individual thumbnails on the Camera preview for each file.

I want to open "/storage/emulated/0/DCIM/Camera" folder using the Files app and further open the photo/video which is not possible with ACTION_GET_CONTENT as it selects the image and exits the Files app as tried here -

    val intent = Intent(Intent.ACTION_GET_CONTENT)
    val uri: Uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path.toString() + "/Camera")
    intent.setDataAndType(uri, "*/*")
    startActivity(Intent.createChooser(intent, "Open folder"))

I tried ACTION_VIEW too, but it is not specific to one folder and opens the gallery showing all media as tried here -

    val intent = Intent()
    intent.action = Intent.ACTION_VIEW
    intent.type = "image/*"
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    startActivity(intent)

"image/*" shows images and videos too in the gallery for me which is good. When "*/*" is used we can use the Files app too but it opens the downloads folder. One solution I found works only with ES Explorer as tried here -

    val uri: Uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path.toString() + "/Camera")
    val intent = Intent(Intent.ACTION_VIEW)
    intent.setDataAndType(uri, "resource/folder")
    startActivity(intent)

This is due to "resource/folder" not supported leading to a crash. Changing "resource/folder" to "*/*" makes Files app open the downloads folder and Photos app to hang.

It seems gallery can do it via buckets , but it too is not universal.

I am not asking for much, just to display my Camera folder from where I can open and view any photo/video.

It was not possible if the user had not once selected a file using ACTION_GET_CONTENT was the opinion.

But now... try this code:

String scheme = "content://com.android.externalstorage.documents/document/primary%3APictures";
//  String scheme = "content://com.android.externalstorage.documents/document/primary%3ADCIM%2FCamera";

Uri uri = Uri.parse(scheme);

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

intent.setType("*/*");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri); // Added in API level 26 

startActivityForResult(intent, 12345);

Toast.makeText(context, "Picker opened in:\n\n" + uri.toString(), Toast.LENGTH_LONG).show();

It works here and i'm pretty amazed.

To open the Files app starting with a certain folder you can use below code but only for Android 11 devices sadly.

//String scheme = "content://com.android.externalstorage.documents/document/primary%3APictures";
//String scheme = "content://com.android.externalstorage.documents/document/primary%3ADownload";
//String scheme = "content://com.android.externalstorage.documents/document/10F9-2E19%3ADownload";
String scheme = "content://com.android.externalstorage.documents/document/primary%3ADCIM%2FCamera";

Uri uri = Uri.parse(scheme);

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(uri, "vnd.android.document/root"); 

startActivity(intent );

Toast.makeText(context, "Files app opening in:\n\n" + uri.toString(), Toast.LENGTH_LONG).show();

                

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM