【发布时间】:2017-01-06 20:36:00
【问题描述】:
我遇到了一个问题,我从未遇到过通过ACTION_VIEW 下一个方式打开文件的问题:
File file = new File(getActivity().getFilesDir(), TEMP_FILE_NAME);
String dataType = "image/*";
if (file.exists()) {
Intent fileIntent = new Intent(Intent.ACTION_VIEW);
fileIntent.setDataAndType(Uri.fromFile(file), dataType);
fileIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(fileIntent, "Open file");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "There is a problem when opening the file");
}
} else {
Toast.makeText(getContext(), "Invalido", Toast.LENGTH_LONG).show();
}
我现在遇到的问题是,即使当我选择应用程序打开文件时文件存在,它也会立即关闭并告诉我Not found。我已将要加载的图像放在图像视图中并且没有问题,因此该文件是有效的,但由于某种原因,当我通过意图打开它时它会发生冲突。
我知道这可能与我创建文件的方式有关,我正在从 Google 驱动器中检索它,因此我正在使用 Apache Commons 库编写文件:
DriveContents contents = result.getDriveContents();
InputStream inputStream = contents.getInputStream();
File file = new File(getActivity().getFilesDir(), TEMP_FILE_NAME);
try {
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(inputStream, outputStream);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
我做错了什么?我不完全确定问题是否与异步执行的复制方法或类似的事情有关。
提前致谢。
【问题讨论】:
标签: android file android-intent inputstream fileoutputstream