2

I have tried to open particular folder in gallary as below code but it didn't work for me, and getting error for Unable to find item.

fun openDirectoryInGallery(context: Context, directory: String) {
        val intent = Intent(Intent.ACTION_VIEW)
        val file = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), directory)
        } else {
            File(Environment.DIRECTORY_PICTURES.plus(File.separator).plus(directory))
        }

        intent.setDataAndType(Uri.withAppendedPath(Uri.fromFile(file), directory), "*/*")
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        startActivity(context, Intent.createChooser(intent, "Open folder"), Bundle())
    }
Vishal Patoliya ツ
  • 3,170
  • 4
  • 24
  • 45
  • 1
    I am not quite sure if it allows you to open a specific folder in gallery since there will be multiple apps listed with chooser intent and its possible that none of them can handle the intent to open a specific folder. I'd suggest you to scan that folder and create your own gallery view. That way it'll be consistent across all the devices. – Shivam Pokhriyal Jan 03 '20 at 06:56
  • @ShivamPokhriyal I have found solution but its not working https://stackoverflow.com/questions/44858799/how-to-open-default-gallery-app-with-particular-album-or-folder/44858906#44858906 – Vishal Patoliya ツ Jan 03 '20 at 07:17
  • that solution work on `2017`, doesn't mean that work on `2020` also. – KuLdip PaTel Jan 03 '20 at 07:21
  • As I already mentioned, this approach doesn't make sure that the gallery app will open the folder that you've specified. Think of it like this: Suppose you've build a gallery app that'll show all photos in your mobile and some other developer wants to open a specific folder in gallery and uses above intent. Now, your app will also be listed in the chooser intent but you haven't handled the intent to show the specific folder. If your app is chosen it'll show all the photos instead. That's why I told you to build your own gallery as this behavior might not be consistent over all devices. – Shivam Pokhriyal Jan 03 '20 at 07:22
  • I hope this link can help you out. https://stackoverflow.com/questions/13418807/how-can-i-display-images-from-a-specific-folder-on-android-gallery – Shivam Pokhriyal Jan 03 '20 at 07:28

2 Answers2

3

There has never been support for this in the OS. This is a question of the installed apps on the device.

There has never been a requirement that a device have a gallery app that is able to respond to ACTION_VIEW for a directory.

Your app should be crashing with a FileUriExposedException on Android 7.0+, and the existence of that exception is why fewer and fewer apps are bothering to support the file scheme.

On Android 10+, the file scheme (and Uri.fromFile() by extension) is simply pointless, as if your app can access the file, other apps cannot.

Finally, Environment.DIRECTORY_PICTURES.plus(File.separator).plus(directory) is not a valid filesystem path.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Looks like grate answer, can you please help me for conclude, Can we navigate to gallery into particular folder on Android Q and Prior O.S? – Vishal Patoliya ツ Jan 03 '20 at 12:20
  • There is no reliable way to do that on any version of Android. And on Android 10+, you have no idea if your desired folder even exists. If you want to keep using your unreliable code, you would need to have the user choose the folder via `ACTION_OPEN_DOCUMENT_TREE`, then pray that the device has a gallery app that supports `ACTION_VIEW` of a `content` `Uri` that points to a tree instead of a piece of content. – CommonsWare Jan 03 '20 at 12:27
  • Can you please give reference link for DocumentsContract.EXTRA_INITIAL_URI? Currently passing uri in file://... format in this param but it doesn't navigate inside pictures folder, is there any another Uri format is required for above param ? – Vishal Patoliya ツ Jan 04 '20 at 06:20
  • @VishalPatoliyaツ: "Currently passing uri in file://... format in this param but it doesn't navigate inside pictures folder" -- well, sure. `DocumentsContract` is for the Storage Access Framework, not files. "is there any another Uri format is required for above param ?" -- it has to be a `Uri` that you obtained from the Storage Access Framework, such as `ACTION_OPEN_DOCUMENT`. "Can you please give reference link" -- https://developer.android.com/reference/android/provider/DocumentsContract#EXTRA_INITIAL_URI – CommonsWare Jan 04 '20 at 13:35
  • Thanks for answer but Storage access framework is from android Q but my question is still open with prior of android Q. – Vishal Patoliya ツ Jan 04 '20 at 14:55
  • 1
    @VishalPatoliyaツ: "Storage access framework is from android Q" -- the Storage Access Framework has been around since Android 4.4. – CommonsWare Jan 04 '20 at 15:08
-4
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"SelectPicture"),PICK_IMAGE_REQUEST); 
eagerprince
  • 115
  • 1
  • 5
  • Doesn't answer the question. OP wants to open a specific folder in gallery not entire gallery. – Shivam Pokhriyal Jan 03 '20 at 07:26
  • 1
    @ShivamPokhriyal is correct, it will not solve the problem. And eagerprince kindly understand the question, the solution you have provided will allow to select only images files be it anywhere but, the problem statement is something different. Also **If you can't explain it simply, you don't understand it well enough - by Einstein** Rather then saying it is not my problem here kindly take efforts to explain things. – Shadow Droid Jan 03 '20 at 07:42