2012-12-07 24 views
0

我尝试使用意图操作文件不会打开,但我不能为PDF和图片文件文件和图像文件中的android

对于图像所有的应用程序要崩溃(包括库的应用程序)

对DOC/DOCX我使用的办公套件,但给出了包(了java.lang.RuntimeException)运行时异常。请参阅下面的代码:

Intent intent = new Intent(); 
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      intent.setAction(Intent.ACTION_VIEW); 

if (extension.equalsIgnoreCase("jpg") 
        | extension.equalsIgnoreCase("jpeg") 
        | extension.equalsIgnoreCase("png") 
        | extension.equalsIgnoreCase("bitmap")) { 
       intent.setDataAndType(Uri.parse(fileName), "image/*"); 
      } else if (extension.equalsIgnoreCase("xml") 
        | extension.equalsIgnoreCase("txt") 
        | extension.equalsIgnoreCase("csv")) { 
       intent.setDataAndType(Uri.parse(fileName), "text/*"); 
      } else if (extension.equalsIgnoreCase("mp4") 
        | extension.equalsIgnoreCase("3gp")) { 
       intent.setDataAndType(Uri.parse(fileName), "video/*"); 
      } else if (extension.equalsIgnoreCase("pdf")) { 
       intent.setDataAndType(Uri.parse(fileName), "application/pdf"); 

       System.out.println("Pdf file to open "+fileName); 

      } else if (extension.equalsIgnoreCase("doc") 
        | extension.equalsIgnoreCase("docx")) { 

       intent.setDataAndType(Uri.parse(fileName), "application/word"); 

      } 


      context.startActivity(intent); 

但是,如果我试图通过去文件资源管理器打开这些文件都正确打开文件。

+0

您可以发布您的例外/ logcat的? –

+0

当我试图打开文档/ docx文件与办公套件Office套件给消息丢失
任何人都可以知道如何删除它? –

回答

0

我认为它的文件URI类的问题没有返回文件 我发现错误

这是Uri.parse(文件名)

我现在用Uri.fromFile(文件名)其工作现在。

1

试试这个:

Intent intent = new Intent(Intent.ACTION_VIEW); 

     //intent.setAction(Intent.ACTION_VIEW); 

     if (extension.equalsIgnoreCase("jpg") 
       | extension.equalsIgnoreCase("jpeg") 
       | extension.equalsIgnoreCase("png") 
       | extension.equalsIgnoreCase("bitmap")) { 
      intent.setDataAndType(Uri.parse("file://"+fileName), "image/*"); 
     } else if (extension.equalsIgnoreCase("xml") 
       | extension.equalsIgnoreCase("txt") 
       | extension.equalsIgnoreCase("csv")) { 
      intent.setDataAndType(Uri.parse("file://"+fileName), "text/*"); 
     } else if (extension.equalsIgnoreCase("mp4") 
       | extension.equalsIgnoreCase("3gp")) { 
      intent.setDataAndType(Uri.parse("file://"+fileName), "video/*"); 
     }else if(extension.equalsIgnoreCase("pdf")){ 
      intent.setDataAndType(Uri.parse("file://"+fileName), "application/pdf"); 
     }else if(extension.equalsIgnoreCase("doc") 
       | extension.equalsIgnoreCase("docx")){ 
      intent.setDataAndType(Uri.parse("file://"+fileName), "text/*"); 
     } 
     // else 
     // if(extension.equalsIgnoreCase("mp3")|extension.equalsIgnoreCase("amr")|extension.equalsIgnoreCase("wav")){ 
     // intent.setDataAndType(Uri.parse(fileName), "audio/mp3"); 
     // } 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     context.startActivity(intent);