简体   繁体   English

Android-如何检查文件管理器是否可用?

[英]Android - How to check if file manager is available?

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. 这段代码在我的genymotion仿真器和CyanogenMod上的Nexus 4上都可以完美运行,并调用文件管理器浏览到该文件夹​​。

But when I try on my brand new LG G4, it doesn't work. 但是,当我尝试使用全新的LG G4时,它不起作用。 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) (如果需要,提供一个简单的替代方法)

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. 然后,检查list的大小(如果为0),则没有文件浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM