出于某种原因,我打开任何下载文件的代码,在这种情况下,.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/