Android - How to check if file manager is available?

泄露秘密 提交于 2019-12-08 09:45:42

问题


My app is using a custom type of file that can be saved onto the external memory and shared.

I want to use the file picker to pick one file and load it. Here is my code :

Uri uri = Uri.fromFile("path/to/folder");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
try {
    startActivityForResult(Intent.createChooser(intent, "Select a file"), 123);
} catch (android.content.ActivityNotFoundException e) {
    Toast.makeText(this, "Error fileChooser",Toast.LENGTH_SHORT).show();
    Log.e("Dan", "onOptionsItemSelected: Error filechooser ActivityNotFoundException", e);
}

This code works absolutely perfectly on my genymotion emulator and my Nexus 4 on CyanogenMod and call the file manager to browse to the folder.

But when I try on my brand new LG G4, it doesn't work. The biggest problem is that I get no error message, just a file picker with the good title and a message "No app can perform this action".

How to check if a file picker is available? (to provide a simple alternative if needed)


回答1:


Try this:

PackageManager packageManager = getActivity().getPackageManager();
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.setType("file/*");
List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                                PackageManager.GET_ACTIVITIES));

Then, check the size of the list, if it is 0, then there is no file explorer.



来源:https://stackoverflow.com/questions/34481929/android-how-to-check-if-file-manager-is-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!