1

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.

zeitgeist
  • 852
  • 12
  • 19
  • I am willing to add a bounty on this question but cannot find the option. – zeitgeist Dec 26 '20 at 09:32
  • We wait until you found the option and we know the bounty ;-). – blackapps Dec 26 '20 at 11:07
  • I found that I can add a bounty after 2 days but the minimum reputation is 50. https://stackoverflow.com/help/bounty. I guess I need to spend more time here for that! – zeitgeist Dec 26 '20 at 11:13
  • Yes, you could spend some time here reading some stackoverflow pages tagged 'android' as what you want has been asked before. – blackapps Dec 26 '20 at 11:18
  • But.. You can let the picker open in the same folder as where the user picked a file before. If you put the obtained content scheme uri in an extra for the next intent then it works. Google for INITIAL_URI. – blackapps Dec 26 '20 at 11:22
  • I have read replies to similar questions but they are not applicable for my usecase. I think you mean https://developer.android.com/reference/android/provider/DocumentsContract#EXTRA_INITIAL_URI. It is applicable only to Intent#ACTION_OPEN_DOCUMENT Intent#ACTION_CREATE_DOCUMENT Intent#ACTION_OPEN_DOCUMENT_TREE but I do not want to pick anything. I want to open like Files app or Gallery normally would. – zeitgeist Dec 26 '20 at 12:14

2 Answers2

0

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.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • It didn't solve my problem as I can only view the thumbnails and if I click them, they get selected and returned, rather than opening. That's the problem with ACTION_GET_CONTENT. – zeitgeist Dec 26 '20 at 12:53
  • 1
    Of course. Thats how it is with ACTION_GET_CONTENT. It is for picking files. Has nothing to do with opening in a certain folder. Normal behaviour. I think it was unclear to me what you wanted. – blackapps Dec 26 '20 at 12:58
  • I just want the Files app to open in the DCIM/Camera folder – zeitgeist Dec 26 '20 at 13:00
  • Well try this with action_view and report. – blackapps Dec 26 '20 at 13:00
  • On lot of devices there is no Files app to begin with. – blackapps Dec 26 '20 at 13:01
  • I have described that behaviour in my question, it does not open a specific folder. Files app just opens the downloads folder and Photos app hangs – zeitgeist Dec 26 '20 at 13:02
  • There is some Files viewer in all of my relevant devices but not ES Explorer. If the default Files or Gallery can open the folder it is good. – zeitgeist Dec 26 '20 at 13:04
0

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

                
blackapps
  • 8,011
  • 2
  • 11
  • 25