How to open specific folder in the storage using intent in android

Viewed 6471

I have tried so many answers related to this question but didn't work any of them. this my code but it didn't open the myFile folder. Please help me to resolve this issue

val intent = Intent(Intent.ACTION_GET_CONTENT)
      val uri = Uri.parse(
            (Environment.getExternalStorageDirectory().absolutePath) + "/myFile/")
      intent.setDataAndType(uri, "*/*")
      startActivityForResult(intent, WRITE_ACCESS_CODE)

This is the result of this code. Only open the recent page

4 Answers

This code may help you:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, YOUR_RESULT_CODE);

If you want to open a specific folder:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

intent.setDataAndType(Uri.parse(Environment.getExternalStorageDirectory().getPath()
 +  File.separator + "myFolder" + File.separator), "file/*");
startActivityForResult(intent, YOUR_RESULT_CODE);

try this -->

  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
     +  File.separator + "myFolder" + File.separator);
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));

OR

// location = "/sdcard/my_folder";
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri mydir = Uri.parse("file://"+location);
intent.setDataAndType(mydir,"application/*");    // or use */*
startActivity(intent);

It worked for me after API 29:

  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q)
        {
            StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);

            Intent intent = sm.getPrimaryStorageVolume().createOpenDocumentTreeIntent();
            //String startDir = "Android";
            //String startDir = "Download"; // Not choosable on an Android 11 device
            //String startDir = "DCIM";
            //String startDir = "DCIM/Camera";  // replace "/", "%2F"
            //String startDir = "DCIM%2FCamera";
            String startDir = "Documents";

            Uri uri = intent.getParcelableExtra("android.provider.extra.INITIAL_URI");

            String scheme = uri.toString();

            Log.d(TAG, "INITIAL_URI scheme: " + scheme);

            scheme = scheme.replace("/root/", "/document/");

            scheme += "%3A" + startDir;

            uri = Uri.parse(scheme);

            intent.putExtra("android.provider.extra.INITIAL_URI", uri);

            Log.d(TAG, "uri: " + uri.toString());

            ((Activity) context).startActivityForResult(intent, REQUEST_ACTION_OPEN_DOCUMENT_TREE);

        }

If you use Kotlin, please see this way:

val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.setDataAndType(data?.toUri(), "application/vnd.ms-excel") // or application/*
startActivity(Intent.createChooser(intent, "Open folder"))

Where data is the location of your file.

Related