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

时间:2014-06-02 12:41:59

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

出于某种原因,我打开任何下载文件的代码,在这个例子中.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):at 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):在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是唯一可以打开所有内容的应用程序。

2 个答案:

答案 0 :(得分: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());
                }                   
            }

答案 1 :(得分:0)

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

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

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