簡體   English   中英

如何使用 android 中的意圖打開存儲中的特定文件夾

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

我已經嘗試了很多與這個問題相關的答案,但沒有一個有效。 這是我的代碼,但它沒有打開 myFile 文件夾。 請幫我解決這個問題

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

這是這段代碼的結果。只打開最近的頁面

試試這個 -->

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

此代碼可以幫助您:

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

如果您使用Kotlin ,請查看此方式:

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

其中數據是您的文件的位置

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