Android 使用startActivity(意图)打开文档-打开的文档错误

Android 使用startActivity(意图)打开文档-打开的文档错误,android,android-intent,Android,Android Intent,通过调用startActivity(intent)在Android中使用Office应用程序(Word和Excel)打开文档时,Office for Android会显示先前显示的文档,而Google doc会显示正确的文档 我如何解决这个问题 文档由Word 2016和Excel 2016(Windows)创建,可以从Google Drive打开,不会出现任何错误。我设定 intent.setDataAndType(uri,“应用程序/vnd.ms excel”)和intent.setData

通过调用
startActivity(intent)
在Android中使用Office应用程序(Word和Excel)打开文档时,Office for Android会显示先前显示的文档,而Google doc会显示正确的文档

我如何解决这个问题

文档由Word 2016和Excel 2016(Windows)创建,可以从Google Drive打开,不会出现任何错误。我设定
intent.setDataAndType(uri,“应用程序/vnd.ms excel”)
intent.setDataAndType(uri,“应用程序/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
私有类AsyncXlsLoader扩展了AsyncTask{
private final ProgressDialog=新建ProgressDialog(getContext());
私有文件tempFile;
@凌驾
受保护的void onPostExecute(整数结果){
super.onPostExecute(结果);
dialog.dismise();
如果(结果==0){
试一试{
意向目标=新意向(意向.行动\视图);
Intent=Intent.createChooser(目标,“打开文件”);
setAction(android.content.intent.ACTION\u视图);
intent.setFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
Uri=FileProvider.getUriForFile(getContext(),“com.mydomain.FileProvider”,tempFile);
setDataAndType(uri,“application/vnd.ms excel”);
intent.addFlags(intent.FLAG\授予\读取\ URI\权限);
星触觉(意向);
}捕获(例外e){
Toast.makeText(getContext(),例如toString(),
吐司。长度(短)。show();
}
}否则{
Toast.makeText(getContext(),“无数据!”,
吐司。长度(短)。show();
}
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
setMessage(“下载”+xlsfile+”);
dialog.show();
}
@凌驾
受保护的整数doInBackground(字符串…参数){
整数结果=新整数(0);
URL u;
xlsDir=getContext().getFilesDir();
试一试{
u=新URL(参数[0]);
HttpURLConnection conn=(HttpURLConnection)u.openConnection();
连接();
//阅读溪流
tempFile=新文件(xlsDir+“/xlsxfiles/”,tempXlsName);
DeleteFile(tempFile,tempXlsName);
if(tempFile.getParentFile().mkdirs()){
tempFile.createNewFile();
}
FileOutputStream fos=新的FileOutputStream(tempFile);
InputStream=u.openStream();
字节[]b=新字节[1024];
整数长度=0;
而((长度=is.read(b))!=-1){
fos.write(b,0,长度);
}
fos.flush();
fos.close();
is.close();
连接断开();
返回0;
}
catch(异常的异常名称格式不正确){
u=零;
返回-1;
}
捕获(可丢弃的t){
t、 printStackTrace();
返回-1;
}
}
}//结束异步xlsLoader

请提供。文档由Word 2016和Excel 2016(Windows)创建,可以从Google Drive中打开,不会出现任何错误。我设置intent.setDataAndType(uri,“application/vnd.msExcel”);setDataAndType(uri,“应用程序/msword”);在我的代码中。欢迎使用堆栈溢出!我已将你的评论编辑到你的问题中。你可以自己做这件事,通过使用你帖子下面的“编辑”链接。在这里提问时,最好在帖子中添加一个。在这种特殊情况下,您可能还会在LogCat中找到相关信息——如果是,请包括LogCat的相关部分。祝你好运请提供。文档由Word 2016和Excel 2016(Windows)创建,可以从Google Drive中打开,不会出现任何错误。我设置intent.setDataAndType(uri,“application/vnd.msExcel”);setDataAndType(uri,“应用程序/msword”);在我的代码中。欢迎使用堆栈溢出!我已将你的评论编辑到你的问题中。你可以自己做这件事,通过使用你帖子下面的“编辑”链接。在这里提问时,最好在帖子中添加一个。在这种特殊情况下,您可能还会在LogCat中找到相关信息——如果是,请包括LogCat的相关部分。祝你好运