Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
91 views
in Technique[技术] by (71.8m points)

java - How to target a specific folder to open in file picker

I am trying to make an app to view files in a folder (the apps storage folder on external storage located at /storage/emulated/0/Android/data/app_name)

However I can not view the folder. I would like to automatically navigate to this folder when the user clicks the button Setting the root path does nothing.

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.jaiselrahman.filepicker.activity.FilePickerActivity;
import com.jaiselrahman.filepicker.config.Configurations;
import com.jaiselrahman.filepicker.model.MediaFile;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    Button btFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btFile = findViewById(R.id.button);

        btFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, FilePickerActivity.class);
                intent.putExtra(FilePickerActivity.CONFIGS,
                        new Configurations.Builder()
                                .setCheckPermission(true)
                                .setShowFiles(true)
                                .setShowImages(false)
                                .setShowAudios(false)
                                .setShowVideos(false)
                                .setMaxSelection(1)
                                .setSuffixes("txt","pdf","doc","docx")
                                .setSkipZeroSizeFiles(true)
                                .setRootPath("/storage/emulated/0/Android/data/")
                                .build());
                startActivityForResult(intent,102);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK && data !=null){
            ArrayList<MediaFile> mediaFiles = data.getParcelableArrayListExtra(
                FilePickerActivity.MEDIA_FILES
            );
            String path = mediaFiles.get(0).getPath();
            if(requestCode == 102){
                String s = "File Path : " + path;
                Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
            }
        }
    }
}
question from:https://stackoverflow.com/questions/65866180/how-to-target-a-specific-folder-to-open-in-file-picker

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...