Android打开各种类型的文件方法总结

通过调用系统的intent打开各种文件的例子,在此记录一下,大家用到的时候可以参考一下:

    /*
    * 根据文件名判断文件类型并返回对应的Intent,之后用startActivity()调用该Intent即可打开对应的文件
    * */
    public static Intent openFile(String filePath){

        File file = new File(filePath);
        if(!file.exists()) return null;
        //  取得扩展名
        String end=file.getName().substring(file.getName().lastIndexOf(".") + 1,file.getName().length()).toLowerCase();
        //  依扩展名的类型返回对应的Intent
        if(end.equals("m4a")||end.equals("mp3")||end.equals("mid")||end.equals("xmf")||end.equals("ogg")||end.equals("wav")){
            return getAudioFileIntent(filePath);
        }else if(end.equals("3gp")||end.equals("mp4")){
            return getAudioFileIntent(filePath);
        }else if(end.equals("jpg")||end.equals("gif")||end.equals("png")||end.equals("jpeg")||end.equals("bmp")){
            return getImageFileIntent(filePath);
        }else if(end.equals("apk")){
            return getApkFileIntent(filePath);
        }else if(end.equals("ppt")){
            return getPptFileIntent(filePath);
        }else if(end.equals("xls")){
            return getExcelFileIntent(filePath);
        }else if(end.equals("doc")){
            return getWordFileIntent(filePath);
        }else if(end.equals("pdf")){
            return getPdfFileIntent(filePath);
        }else if(end.equals("chm")){
            return getChmFileIntent(filePath);
        }else if(end.equals("txt")){
            return getTextFileIntent(filePath,false);
        }else{
            return getAllIntent(filePath);
        }

    }


下面是不同类型Intent的获取方法:

    //  Android获取一个用于打开所有类型文件的intent
    public static Intent getAllIntent( String param ) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri,"*/*");
        return intent;
    }

    //  Android获取一个用于打开APK文件的intent
    public static Intent getApkFileIntent( String param ) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri,"application/vnd.android.package-archive");
        return intent;
    }

    //  Android获取一个用于打开VIDEO文件的intent
    public static Intent getVideoFileIntent( String param ) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("oneshot", 0);
        intent.putExtra("configchange", 0);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "video/*");
        return intent;
    }

    //  Android获取一个用于打开AUDIO文件的intent
    public static Intent getAudioFileIntent( String param ){
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("oneshot", 0);
        intent.putExtra("configchange", 0);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "audio/*");
        return intent;
    }

    //  Android获取一个用于打开Html文件的intent
    public static Intent getHtmlFileIntent( String param ){
        Uri uri = Uri.parse(param ).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param ).build();
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.setDataAndType(uri, "text/html");
        return intent;
    }

    //Android获取一个用于打开图片文件的intent
    public static Intent getImageFileIntent( String param ) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "image/*");
        return intent;
    }

    //  Android获取一个用于打开PPT文件的intent
    public static Intent getPptFileIntent( String param ){
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
        return intent;
    }

    //  Android获取一个用于打开Excel文件的intent
    public static Intent getExcelFileIntent( String param ){
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "application/vnd.ms-excel");
        return intent;
    }

    //  Android获取一个用于打开Word文件的intent
    public static Intent getWordFileIntent( String param ){
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "application/msword");
        return intent;
    }

    //  Android获取一个用于打开CHM文件的intent
    public static Intent getChmFileIntent( String param ){
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "application/x-chm");
        return intent;
    }

    //  Android获取一个用于打开文本文件的intent
    public static Intent getTextFileIntent( String param, boolean paramBoolean){
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (paramBoolean){
            Uri uri1 = Uri.parse(param );
            intent.setDataAndType(uri1, "text/plain");
        }else{
            Uri uri2 = Uri.fromFile(new File(param ));
            intent.setDataAndType(uri2, "text/plain");
        }
        return intent;
    }

    //  Android获取一个用于打开PDF文件的intent
    public static Intent getPdfFileIntent( String param ){
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "application/pdf");
        return intent;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Android中,我们可以使用以下步骤来打开任意位置的文件: 1. 首先,我们需要获取文件的路径。可以是绝对路径,也可以是相对路径。如果您知道文件的绝对路径,可以直接使用该路径。如果您知道文件位于应用的内部存储目录或SD卡的特定位置,可以创建一个相对路径。 2. 接下来,我们需要使用一个适当的方法打开文件。这取决于您要打开的是什么类型的文件。下面是一些常见文件类型打开方式: - 如果您要打开文本文件(例如.txt文件),可以使用`InputStream`读取文件的内容,并将其显示在您的应用程序的UI界面上。 - 如果您要打开图像文件(例如.jpg或.png文件),可以使用`ImageView`来显示图像。 - 如果您要打开音频或视频文件(例如.mp3或.mp4文件),可以使用`MediaPlayer`或`VideoView`来播放文件。 3. 最后,为了使您的应用能够打开任意位置的文件,您需要在AndroidManifest.xml文件中为应用程序添加适当的权限。例如,如果您要访问外部存储器上的文件,需要添加`READ_EXTERNAL_STORAGE`权限。 总结起来,要打开任意位置的文件,您需要获取文件的路径,根据文件类型选择适当的方法打开文件,并在AndroidManifest.xml文件中添加合适的权限。 ### 回答2: 在 Android 设备上,我们可以使用 Intent 来打开任意位置的文件。下面是一个简单的示例: 首先,我们需要在 AndroidManifest.xml 文件中添加适当的权限。例如,如果要打开图片文件,需要添加以下权限: ```xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ``` 接下来,在代码中创建一个 Intent 对象,并指定要执行的操作以及要打开文件的位置。例如,如果要打开图片文件,可以使用以下代码: ```java String filePath = "/mnt/sdcard/Pictures/image.jpg"; // 文件路径 File file = new File(filePath); // 创建 File 对象 Uri uri = Uri.fromFile(file); // 将 File 对象转换为 Uri Intent intent = new Intent(Intent.ACTION_VIEW); // 创建打开文件的 Intent intent.setDataAndType(uri, "image/*"); // 设置要打开文件 Uri 和 MIME 类型 startActivity(intent); // 启动 Intent ``` 这将会打开 Android 设备上与文件关联的默认应用程序。例如,如果文件是图片,则会打开图片查看器应用程序。如果文件是 PDF,则会打开 PDF 阅读器应用程序。 请注意,如果要打开文件位于应用的私有目录中,如内部存储的`getFilesDir()`或外部存储的`getExternalFilesDir()`,则不需要声明额外的权限。 ### 回答3: 在Android上,我们可以使用一些方法打开任意位置的文件。以下是一个示例代码,演示如何打开任意位置的文件: ```java import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import java.io.File; public class MainActivity extends AppCompatActivity { private Button openFileButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); openFileButton = findViewById(R.id.open_file_button); openFileButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { File file = new File(Environment.getExternalStorageDirectory().getPath() + "/path/to/file"); // 替换为你想要打开文件的路径 if (file.exists()) { Uri uri = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); // 替换为你想要打开文件的MIME类型 startActivity(Intent.createChooser(intent, "选择应用打开文件")); } } }); } } ``` 在这个示例中,我们首先获取了文件的路径,并创建了一个File对象。然后,我们检查该文件是否存在。如果文件存在,我们通过将文件的URI传递给一个带有"application/pdf" MIME类型的Intent来调用系统的文件查看器或其他适当的应用程序来打开文件。最后,我们使用startActivity方法来启动该Intent。 这个示例假设我们要打开文件是PDF文件,并替换其中的路径和MIME类型以适应你自己的需求。 当然,要记得在AndroidManifest.xml文件中添加适当的权限,以便应用可以读取外部存储。这可以通过在manifest标签中添加以下权限来实现: ```xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ``` 希望这个回答能够帮助到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值