Android-如何检查文件管理器是否可用? [英] Android - How to check if file manager is available?

查看:203
本文介绍了Android-如何检查文件管理器是否可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用使用的是自定义类型的文件,可以将其保存到外部存储器中并进行共享.

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);
}

此代码在我的genymotion仿真器和CyanogenMod上的Nexus 4上绝对完美地工作,并调用文件管理器浏览到该文件夹​​.

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.

但是当我尝试使用全新的LG G4时,它不起作用. 最大的问题是,我没有收到任何错误消息,只有标题正确的文件选择器和没有应用程序可以执行此操作"消息.

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".

如何检查文件选择器是否可用? (以在需要时提供一种简单的替代方法)

推荐答案

尝试一下:

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

然后,检查list的大小,如果为0,则没有文件浏览器.

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

这篇关于Android-如何检查文件管理器是否可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆