Android 在 QuickOffice 崩溃中通过 ContentResolver 打开 .docx 文件

标签 android android-intent ms-word android-contentprovider document

出于某种原因,我打开任何下载文件的代码,在这种情况下,.docx 不适用于 QuickOffice,我得到的异常是:

06-02 22:04:05.356: E/AndroidRuntime(2889): 致命异常: main 06-02 22:04:05.356:E/AndroidRuntime(2889):进程:com.quickoffice.android,PID:2889 06-02 22:04:05.356: E/AndroidRuntime(2889): java.lang.NullPointerException 06-02 22:04:05.356:E/AndroidRuntime(2889):在 com.google.android.apps.docs.quickoffice.a.g.a(AbstractSaveAction.java:14) 06-02 22:04:05.356:E/AndroidRuntime(2889):在 com.google.android.apps.docs.editors.menu.d.a(AbstractMenuItemController.java:28) 06-02 22:04:05.356:E/AndroidRuntime(2889):在 com.google.android.apps.docs.editors.menu.c.a(AbstractButtonMenuItemController.java:24) 06-02 22:04:05.356:E/AndroidRuntime(2889):在 com.google.android.apps.docs.editors.menu.y.a(MenuController.java:59) 06-02 22:04:05.356:E/AndroidRuntime(2889):在 com.google.android.apps.docs.editors.menu.y.a(MenuController.java:59) 06-02 22:04:05.356:E/AndroidRuntime(2889):在 com.google.android.apps.docs.editors.menu.z.run(MenuController.java:37)

我建立 Intent 的代码是:

Intent install = new Intent(Intent.ACTION_VIEW);
String mimeType = getMimeType(tempFile);

Uri uri = Uri.parse("content://com.companyname/"+ fileName);
install.setDataAndType(uri, mimeType);
install.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createChooser(install, "Open File");  
_progressDialog.getContext().startActivity(intent);

我的内容提供商代码是这样的:

    public class DownloadedFileContentProvider extends ContentProvider {

        @Override
        public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
        File privateFile = new File(getContext().getCacheDir(), uri.getPath());
        return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_WRITE);
        }

           //Other overridden methods that return false and null for the ContentProvider class.
     }

我没思路了,android 上的 Microsoft Office 也是这样,文档根本打不开。 Office 套件是唯一打开一切的应用程序。

最佳答案

我是这样做的

            File file = new File(filePath);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
            String fileExtension = filePath.substring(filePath.lastIndexOf(".") + 1);
            fileExtension = fileExtension.toLowerCase();
            if(mimeTypeMap.getMimeTypeFromExtension(fileExtension) != null) {
                String type = mimeTypeMap.getMimeTypeFromExtension(fileExtension);

                if(isTablet(activity))
                {
                    if(fileExtension.equals("txt"))
                    {
                        intent.setDataAndType(Uri.fromFile(file), "text/plain");    //changed from Uri.parse(fp, type) method
                    }
                    else
                    {
                        intent.setDataAndType(Uri.fromFile(file), type);
                    }
                }
                else
                {
                    intent.setDataAndType(Uri.fromFile(file), type);
                }                   

                try{
                    activity.startActivityForResult(intent, Constants.REQUEST_AUTHENTICATION_COMPLETED);
                }catch(android.content.ActivityNotFoundException e){
                    System.out.println("exception : "+e.getLocalizedMessage());
                }                   
            }

关于Android 在 QuickOffice 崩溃中通过 ContentResolver 打开 .docx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23994815/

相关文章:

android - XMPP aSmack MultiUserChat 服务器继续发送已收到的消息

android - 错误:任务 ':app:compileDebugKotlin'的执行失败

android - 如何使用可重用 Activity 在我的应用程序中定义不同的 Activity 流?

regex - 在 MS word 中查找包含单词的段落

iphone - 将视频流式传输(或渐进式下载)到 Droid 浏览器

android - 在 Fragment 中添加内容之前必须调用 requestFeature()

android - AppWidget 第一次添加时没有完成更新

android - 在 Android 应用中提交带有 POST 数据的表单

ms-word - Apache POI HWPF - 将表格输出到 Microsoft Word

html - 如何将 Microsoft word para/range 转换为 html?