【问题标题】:Android open .docx file via ContentResolver in QuickOffice crashesAndroid 在 QuickOffice 崩溃中通过 ContentResolver 打开 .docx 文件
【发布时间】:2014-06-19 21:15:30
【问题描述】:

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

06-02 22:04:05.356:E/AndroidRuntime(2889):致命异常:主要 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): at 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): at com.google.android.apps.docs.editors.menu.c.a(AbstractButtonMenuItemController.java:24) 06-02 22:04:05.356: E/AndroidRuntime(2889): at com.google.android.apps.docs.editors.menu.y.a(MenuController.java:59) 06-02 22:04:05.356: E/AndroidRuntime(2889): at com.google.android.apps.docs.editors.menu.y.a(MenuController.java:59) 06-02 22:04:05.356: E/AndroidRuntime(2889): at com.google.android.apps.docs.editors.menu.z.run(MenuController.java:37)

我建立意图的代码是:

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 Suite 是唯一可以打开一切的应用程序。

【问题讨论】:

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


【解决方案1】:

我是这样做的

            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());
                }                   
            }

【讨论】:

    【解决方案2】:

    QuickOffice 应用程序崩溃,因为它调用了 ContentProvider 的 query 方法。由于此方法是抽象的,您必须在自定义 ContentProvider 中实现它,但可能返回 null 或抛出异常。正如 QuickOffice 在尝试使用您的内容时调用的那样,它崩溃了(可能出现 NullPointerException)。

    正如 CommonsWare 在他对原始帖子的评论中所建议的那样,您应该改用 FileProvider。 Android 文档清楚地说明了如何使用它。您不需要使用任何 Java 代码,一切都在 XML 文件(Manifest 和路径 XML 文件)中完成。

    我遇到了同样的问题。使用 FileProvider 使其以最简单、更安全的方式工作。谢谢CommonsWare。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 2011-09-06
      • 1970-01-01
      • 2017-07-06
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多