由于某种原因,我用来打开任何下载文件的代码,在这种情况下,.docx不能在QuickOffice上工作,我得到的异常是:
06-02 22:04:05.356: E/AndroidRuntime(2889):致命异常: main 06-02 22:04:05.356: E/AndroidRuntime(2889):进程: com.quickoffice.android,com.google.android.apps.docs.quickoffice.a.g.a(AbstractSaveAction.java:14):288906-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 java.lang.NullPointerException 06-02 22:04:05.356: E/AndroidRuntime(2889):at PID 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在com.google.android.apps.docs.editors.menu.z.run(MenuController.java:37)运行时( .y.a(MenuController.java:59) 06-02 22:04:05.356: E/AndroidRuntime)
我用来建立意图的代码是:
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是唯一可以打开所有内容的应用程序。
发布于 2014-06-02 20:52:41
我就是这样做的
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());
}
}发布于 2014-06-19 21:15:31
QuickOffice应用程序崩溃是因为它调用了ContentProvider的query方法。由于此方法是抽象的,因此您必须在自定义ContentProvider中实现它,但可能使用null返回或抛出异常。当试图使用你的内容时,QuickOffice调用它,它崩溃了(可能是用NullPointerException)。
正如CommonsWare在他对原始帖子的评论中所建议的那样,你应该使用FileProvider。Android文档清楚地说明了如何使用它。您不需要使用任何Java代码,所有操作都在XML文件(Manifest和路径XML文件)中完成。
我也有同样的问题。使用FileProvider使它以一种最简单和更安全的方式工作。谢谢你CommonsWare。
https://stackoverflow.com/questions/23994815
复制相似问题