0

我正在开发一个 android 图库应用程序,所以当我单击文件选择器按钮时,我只想打开图像/照片文件夹,

我正在尝试打开它,但所有不必要的文件夹都显示打开的活动(音乐/视频/联系人/.etc)。我只想打开该图像和文件文件夹。

这是我的文件选择器代码。我正在使用 ACTION_PICK 进行公开活动

 public void showFileChooser(View v) {

            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("/*");

            if(Build.VERSION.SDK_INT >= 15 && Build.VERSION.SDK_INT <= 18){
                intent.putExtra(Intent.EXTRA_LOCAL_ONLY,true);
                intent.setAction(Intent.ACTION_GET_CONTENT);

                try {
                    startActivityForResult(
                            Intent.createChooser(intent, "Select Picture"),FILE_SELECT_CODE);
                } catch (ActivityNotFoundException ex) {
                    // Potentially direct the user to the Market with a Dialog
                    Toast.makeText(this, "Please install a File Manager.",
                            Toast.LENGTH_SHORT).show();
                }

            }

           else if(Build.VERSION.SDK_INT >= 19){
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
                intent.putExtra(Intent.EXTRA_LOCAL_ONLY,true);
                intent.setAction(Intent.ACTION_PICK);

                try {
                    startActivityForResult(
                            Intent.createChooser(intent, "Select Picture"),FILE_SELECT_CODE);
                } catch (ActivityNotFoundException ex) {
                    // Potentially direct the user to the Market with a Dialog
                    Toast.makeText(this, "Please install a File Manager.",
                            Toast.LENGTH_SHORT).show();
                }
            }

           else {
               Log.e("no","no");
            }

}

我想这样展示

谢谢你。

4

1 回答 1

1

尝试这个 :

if (Build.VERSION.SDK_INT >= 23) {
                    if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);
                    } else {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction("android.intent.action.PICK");
                        startActivityForResult(Intent.createChooser(intent, "Select picture using"), 101);
                    }
                } else {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction("android.intent.action.PICK");
                    startActivityForResult(Intent.createChooser(intent, "Select picture using"), 101);
                }

如果你想要一个文件夹列表,那么你可以使用这个库之一

https://github.com/topics/android-image-picker

于 2019-11-08T05:26:08.770 回答