如何使用android中已安装的应用程序打开文件?

如何使用android中已安装的应用程序打开文件?,android,android-intent,Android,Android Intent,我已经编写了一段代码,可以检测android中已安装的应用程序,并用该应用程序打开文件。例如,一个word文档应该用一些office apk打开 Intent intent = new Intent(android.content.Intent.ACTION_VIEW); Uri data = Uri.fromFile(temp_file); String type = getMimeType(temp_file.getName()); intent.setDataAndType(data, t

我已经编写了一段代码,可以检测android中已安装的应用程序,并用该应用程序打开文件。例如,一个word文档应该用一些office apk打开

Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.fromFile(temp_file);
String type = getMimeType(temp_file.getName());
intent.setDataAndType(data, type);
this.startActivity(intent);
在上面的代码中,temp_file是应该打开的文件

public static String getMimeType(String url) {
        String type = null;
        String extension = MimeTypeMap.getFileExtensionFromUrl(url);
        if (extension != null) {
            MimeTypeMap mime = MimeTypeMap.getSingleton();
            type = mime.getMimeTypeFromExtension(extension);
        }
        return type;
    }
但当我执行时,它会抛出android.content.ActivityNotFoundException异常。那么,我在这里做错了什么吗?

这可能会帮助你

 button.setOnClickListener(new View.OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                        File file=new File("/sdcard/yourfile");
                        if(file.exists())
                        {
                              Uri path=Uri.fromFile(file);
                              Intent intent=new Intent(Intent.ACTION_VIEW);
                              intent.setDataAndType(path, "application/readername");

                              try
                              {

                                    startActivity(intent);
                              }
                              catch(ActivityNotFoundException e)
                              {
                                    Toast.makeText(TestActivity.this, "No software for PDF", Toast.LENGTH_SHORT).show();
                              }
                        }
                  }
            });
  private void readFile(File file){
     Uri path = Uri.fromFile(file);
     Intent intent = new Intent(Intent.ACTION_VIEW);
     intent.setDataAndType(path, "application/pdf");// for .pdf file
         //intent.setDataAndType(path, "application/msword");// for msword file
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 try {
         startActivity(intent);
      } 
     catch (ActivityNotFoundException e) {
         String str=  "No Application Available to View .pdf file.";
         showToast(str);
     }

其中file是要打开的文件名

您正在调用
getMimeType()
并将文件名传递给它。但是您的方法
getMimeType()
需要一个URL。
MimeTypeMap.getFileExtensionFromUrl()
的文档特别说明:

此方法是获取url扩展名的方便方法,对于其他字符串有未定义的结果。

对于mime类型,您可能会得到
null
。添加一些调试日志记录,并检查
getMimeType()
返回的内容


另外,查看日志。它应该告诉您它试图解决的
意图的内容。这也应该给你一个提示。

Thnx David。。n对于其他人,您提供的解决方案仅适用于固定的MIME类型。我已经能够做到这一点。但我想编写适用于所有文件的代码。可能你们都应该考虑将事情概括起来。:)我在上述代码->对getMimeType方法的调用中所做的更正应该是:
String type=getMimeType(data.toString())