Trying open a specific folder in android using int

2020-02-05 03:16发布

问题:

I am trying to open a specific folder in android ?Is it possible to open a specific folder ???? this is the code i m using

    config=(Button)findViewById(R.id.btn_cf);

      config.setOnClickListener(new View.OnClickListener() 
      {         
            @Override
            public void onClick(View v)
            { 

            Intent intent = new Intent("Intent.ACTION_GET_CONTENT"); 
             Uri uri = Uri.parse("mnt/sdcard/myfiles/allfiles/download"); 
            intent.setDataAndType(uri, "*/*"); 
            startActivity(Intent.createChooser(intent, "download"));


            }
       });

回答1:

try to replace your code with this line

  btn.setOnClickListener(new View.OnClickListener() 
      {         
            @Override
            public void onClick(View v)
            { 

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


            }
       });


回答2:

It works:

Uri selectedUri = Uri.parse(Environment.getExternalStorageDirectory() + "/myFolder/");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");
startActivity(intent); 

Have a nice codding :)

EDIT: If the current solution doesn't help you, then these file/directory choosers libraries can be helpful: https://android-arsenal.com/tag/35



回答3:

I found a solution in this GitHub repo

The code :

If you want open & browse file: FileBrowser.class

        Intent intent = new Intent(activity, FileBrowser::class.java)
        intent.putExtra(Constants.INITIAL_DIRECTORY, File(storageDirPath).absolutePath)
        intent.putExtra(Constants.ALLOWED_FILE_EXTENSIONS,"*")

        startActivityForResult(intent, CODE_INTENT )

If you want to get user chosen file's URI: FileChooser.class

        Intent intent = new Intent(activity, FileChooser::class.java)
        intent.putExtra(Constants.INITIAL_DIRECTORY, File(storageDirPath).absolutePath)
        startActivityForResult(intent, CODE_INTENT )


回答4:

Pick root:

Intent selectFile = new Intent(); 
                             selectFile.setAction("com.sec.android.app.myfiles.PICK_DATA_MULTIPLE");    
            selectFile.putExtra("CONTENT_TYPE", "*/*");
            selectFile.addCategory(Intent.CATEGORY_DEFAULT);
            startActivity(selectFile);