【问题标题】:How to open specific folder in the storage using intent in android如何使用android中的意图打开存储中的特定文件夹
【发布时间】:2020-07-08 06:40:45
【问题描述】:

我已经尝试了很多与这个问题相关的答案,但没有一个可以解决。这是我的代码,但它没有打开 myFile 文件夹。请帮我解决这个问题

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

【问题讨论】:

  • tried so many answers related with this question 哪个问题?
  • 我提到的问题。 “如何在 android 中使用 Intent 打开存储中的特定文件夹”
  • 您应该将所有问题和疑问都放在您的帖子中。不在 cmets 或主题或标题中。写一篇像样的帖子。
  • @blackapps 为什么你用问题回答问题,如果你不能帮助你不要说不。

标签: java android kotlin


【解决方案1】:

此代码可能会对您有所帮助:

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

如果要打开特定文件夹:

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

【讨论】:

  • 是的,它有效,非常感谢。另一个问题。我无法打开该文件夹中的文件。是权限问题吗?
  • 也许可以,确保应用有权限并且清单文件正确
  • @Sanush 也许您可以尝试将“file/*”更改为“image/png”之类的内容。我们试过这样可以打开图片文件。
  • 我们已经尝试过此解决方案,但仍然无法打开特定文件夹。还有其他需要注意的因素吗?
【解决方案2】:

试试这个 -->

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

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

【讨论】:

  • 我的两种方法都失败了?
  • 不要使用它会导致android.os.FileUriExposedException
  • @basit9 那么你为android.os.FileUriExposedException做了什么?
  • 好吧,这是因为“file://”而发生的,所以我删除了它,在我的例子中,路径是从“content//:”开始的。完整的解决方案,dir是路径Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setDataAndType(Uri.parse(dir), "/");
【解决方案3】:

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

        }

【讨论】:

    【解决方案4】:

    如果你使用 Kotlin,请这样看:

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

    data 是您文件的位置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-27
      • 1970-01-01
      • 2018-10-13
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多