2017-08-02 24 views
4

我的要求是在我的應用程序中下載和查看Excel,Doc文件。所以我將Excel/Doc文件下載到手機存儲中,並通過傳遞文件路徑來調用ACTION_VIEW Intent。然後我得到這個錯誤,說:「嘗試將文件保存在設備上,然後打開它。」如何從我的應用程序在MS-Excel或MS-Word中打算使用意向打開excel,doc文件 - Android

This is Error ScreenShot

我可以很高興,如果任何人可以建議我另外的替代方案以及打開Excel或DOC文件。請大家,我已經搜索了很多,迄今爲止,我沒有找到解決方案,我相信我已經使用了正確的打算文件的意圖。

在哪裏我的代碼:

Intent intentpdf = new Intent(Intent.ACTION_VIEW); 
       intentpdf.setDataAndType(Uri.parse(message), "application/msword"); 
       intentpdf.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

       try { 
        mactivity.startActivity(intentpdf); 
       } catch (ActivityNotFoundException e) { 


        if (!mactivity.isFinishing()) 
         Toast.makeText(mactivity, "Install MSWord Viewer.", Toast.LENGTH_LONG).show(); 

       } 


Intent intentpdf = new Intent(Intent.ACTION_VIEW); 
       intentpdf.setDataAndType(Uri.parse(message), "application/vnd.ms-excel"); 
       intentpdf.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

       try { 
        mactivity.startActivity(intentpdf); 
       } catch (ActivityNotFoundException e) { 


        if (!mactivity.isFinishing()) 
         Toast.makeText(mactivity, "Install Excel Viewer.", Toast.LENGTH_LONG).show(); 

       } 
+0

你已經解決了這個問題,得到的MIME類型? –

+2

沒有先生?這是打開文件的直接操作。但是,不是它應該工作的方式。 –

+0

這個 –

回答

0

我有這個確切的同樣的問題,我固定它。這個問題很可能與你的ContentProvider/FileProvider實現有關。我的Excel工作表在Google表格中打開,但在MS Excel中出現此錯誤。解決方法是將適當的內容類型(通過getType()方法)從您的提供程序中返回。返回錯誤的內容類型將導致錯誤。

試試這個代碼,看看它是否有幫助...如果是這樣,你可以完全實現所有類型的文件的例程。

@Override 
    public String getType(Uri uri) { 
     return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
    } 

祝你好運!

+0

的任何解決方案,我必須重寫這個? –

+0

在你的ContentProvider實現中 –

0

嘗試從文件

public String getMimeTypeByFile(String filePath) { 
     MimeTypeMap type = MimeTypeMap.getSingleton(); 
     return type.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(filePath)); 
    } 
相關問題