由于某种原因,我打开任何下载文件的代码,在这种情况下.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):在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 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是唯一打开所有内容的应用程序。
最佳答案
我做到了如下
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());
}
}