通过Android上的Intent选择任何类型的文件

ErG*_*404 85 android file intentfilter android-intent

我想为可以返回任何类型文件的应用启动intentchooser

目前我使用(我从Android电子邮件源代码中复制了文件附件)

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
Intent i = Intent.createChooser(intent, "File");
startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);
Run Code Online (Sandbox Code Playgroud)

但它只在我的Galaxy S2上显示"画廊"和"音乐播放器".此设备上有一个文件浏览器,我希望它出现在列表中.我还希望相机应用程序显示在列表中,以便用户可以拍摄照片并通过我的应用程序发送.如果我安装Astro文件管理器,它也会响应这个意图.我的客户只是Galaxy SII所有者,我不想强​​迫他们安装Astro文件管理器,因为他们已经拥有一个基本但足够的文件管理器.

知道如何实现这一目标吗?我很确定我已经看到默认文件管理器出现在这样的菜单中以选择文件,但我不记得在哪个应用程序中.

use*_*305 82

不适用于相机,但适用于其他文件..

在我的设备中,我ES File Explorer安装了,这简单的事情在我的情况下工作..

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
Run Code Online (Sandbox Code Playgroud)

  • 它适用于外部文件资源管理器(例如ES文件资源管理器或Astro文件管理器,但不适用于三星文件浏览器.看起来很奇怪,他们没有实现正确的意图过滤器来响应该操作. (3认同)
  • 我怎样才能获得所选文件?我想这是路径,但是怎么样? (2认同)

Chu*_*pik 46

三星文件浏览器不仅需要自定义操作(com.sec.android.app.myfiles.PICK_DATA),而且部分类(意向.CATEGORY_DEFAULT)和MIME类型应该额外进行传递.

Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
Run Code Online (Sandbox Code Playgroud)

您也可以使用此操作打开多个文件:com.sec.android.app.myfiles.PICK_DATA_MULTIPLE无论如何,这里是我的解决方案,适用于三星和其他设备:

public void openFile(String mimeType) {

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType(mimeType);
        intent.addCategory(Intent.CATEGORY_OPENABLE);

        // special intent for Samsung file manager
        Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
         // if you want any file type, you can skip next line 
        sIntent.putExtra("CONTENT_TYPE", mimeType); 
        sIntent.addCategory(Intent.CATEGORY_DEFAULT);

        Intent chooserIntent;
        if (getPackageManager().resolveActivity(sIntent, 0) != null){
            // it is device with Samsung file manager
            chooserIntent = Intent.createChooser(sIntent, "Open file");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { intent});
        } else {
            chooserIntent = Intent.createChooser(intent, "Open file");
        }

        try {
            startActivityForResult(chooserIntent, CHOOSE_FILE_REQUESTCODE);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(getApplicationContext(), "No suitable File Manager was found.", Toast.LENGTH_SHORT).show();
        }
    }
Run Code Online (Sandbox Code Playgroud)

这个解决方案适合我,也许对其他人有用.


小智 37

这项工作对我来说是Galaxy注意它的显示联系人,文件管理器安装在设备,画廊,音乐播放器上

private void openFile(Int  CODE) {
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.setType("*/*");
    startActivityForResult(intent, CODE);
}
Run Code Online (Sandbox Code Playgroud)

这里onActivityResult有活动的路径.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     String Fpath = data.getDataString();
    // do somthing...
    super.onActivityResult(requestCode, resultCode, data);

}
Run Code Online (Sandbox Code Playgroud)

  • 如何将uri转换为util.File? (6认同)
  • 如何挑选一个文件夹? (3认同)

Isl*_*led 10

这给了我最好的结果:

    Intent intent;
    if (android.os.Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
        intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
        intent.putExtra("CONTENT_TYPE", "*/*");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
    } else {

        String[] mimeTypes =
                {"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx
                        "application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", // .ppt & .pptx
                        "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
                        "text/plain",
                        "application/pdf",
                        "application/zip", "application/vnd.android.package-archive"};

        intent = new Intent(Intent.ACTION_GET_CONTENT); // or ACTION_OPEN_DOCUMENT
        intent.setType("*/*");
        intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    }
Run Code Online (Sandbox Code Playgroud)