通过调用startActivity(intent)
在Android中使用Office应用程序(包括Word,Excel)打开文档时,Office for Android会显示以前显示的文档,而Google doc会显示正确的文档。
我该如何解决这个问题?
文档由Word 2016和Excel 2016(Windows)创建,可以从Google Drive中打开而不会出现任何错误。我设置
我的代码中有intent.setDataAndType(uri, "application/vnd.ms-excel");
和intent.setDataAndType(uri, "application/msword");
。
以下是测试中的代码。
private class AsyncXlsLoader extends AsyncTask<String, Void, Integer> {
private final ProgressDialog dialog = new ProgressDialog(getContext());
private File tempFile;
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
dialog.dismiss();
if (result == 0) {
try {
Intent target = new Intent(Intent.ACTION_VIEW);
Intent intent = Intent.createChooser(target, "Open File");
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Uri uri = FileProvider.getUriForFile(getContext(), "com.mydomain.fileprovider", tempFile);
intent.setDataAndType(uri, "application/vnd.ms-excel");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} catch (Exception e) {
Toast.makeText(getContext(), e.toString(),
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getContext(), " No data!",
Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setMessage("Downloading " + xlsfile + "...");
dialog.show();
}
@Override
protected Integer doInBackground(String... params) {
Integer result = new Integer(0);
URL u;
xlsDir = getContext().getFilesDir();
try{
u = new URL(params[0]);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.connect();
// Read the stream
tempFile = new File(xlsDir + "/xlsxfiles/", tempXlsName);
DeleteFile(tempFile, tempXlsName);
if (tempFile.getParentFile().mkdirs()) {
tempFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(tempFile);
InputStream is = u.openStream();
byte[] b = new byte[1024];
int length = 0;
while ((length = is.read(b)) != -1) {
fos.write(b,0, length);
}
fos.flush();
fos.close();
is.close();
conn.disconnect();
return 0;
}
catch(MalformedURLException nameOfTheException){
u = null;
return -1;
}
catch (Throwable t){
t.printStackTrace();
return -1;
}
}
} // End AsyncXlsLoader