简体   繁体   English

如何使用 android 中的意图打开存储中的特定文件夹

[英]How to open specific folder in the storage using intent in android

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.这是我的代码,但它没有打开 myFile 文件夹。 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)

这是这段代码的结果。只打开最近的页面

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

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

If you use Kotlin , please see this way:如果您使用Kotlin ,请查看此方式:

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.其中数据是您的文件的位置

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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