从我的Android活动中打开文件选择器

Onh*_*ron 3 android actionview sd-card android-intent

我正在制作一个Android应用程序,它将一些下载的pdf文件存储在设备的SD卡中.一切正常,但现在我想添加一个功能,只需弹出默认的android文件/文件夹浏览器,显示我的应用程序存储所有PDF(包含子目录)的目录,以便用户可以看到他的文档存储位置和可以轻松浏览它们.

我经历了很多其他SO问题和论坛帖子,但似乎这只能用于音乐/图像/联系人等.基本上那些具有"专用浏览系统"但不具有一般文件浏览功能的文件类型.

我实际上是在使用这段代码:

File file = new File("/sdcard/MySorgenia/Documenti/");
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri data = Uri.fromFile(file);
String type = "*/*";
intent.setDataAndType(data, type);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

但这将显示一个"选择应用程序以完成您的操作"对话框,其中包含许多应用程序,如"音乐""图库"等,但没有通用目的.

谢谢!

use*_*305 9

因为在Android中没有任何本机应用程序,您可以将其用作文件资源管理器并进行响应 Intent type "*/*"

File-Explorer为此目的实现您自己的代码..

看看这两个链接..

  1. OpenIntents软件

  2. Android的文件,浏览


小智 6

public void loadfile()
{
    private static final int gallery=12; 
    private static final String type="*/*";
    
    Intent i=new Intent(Intent.ACTION_GET_CONTENT);
    i.setType(type);
    startActivityForResult(Intent.createChooser(i,"select file"), gallery);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == gallery && resultCode == RESULT_OK && data != null) {
        Uri uploadfileuri = data.getData();
        File file = new File(uploadfileuri.getPath());
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 请对Ans进行一些描述。 (2认同)