【问题标题】:[Android ]Intent.ACTION_VIEW - Not found[Android]Intent.ACTION_VIEW - 未找到
【发布时间】: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


    【解决方案1】:

    我从来没有遇到过通过 ACTION_VIEW 以另一种方式打开文件的问题

    该代码永远不会起作用,因为第三方应用无权使用您应用的getFilesDir() 上的文件。

    我做错了什么?

    您正在尝试向第三方程序提供无法访问的文件。 Use FileProvider 提供文件,使用 FileProvider.getUriForFile() 获取 Uri 以在您的 ACTION_VIEW Intent 中使用。

    【讨论】:

    • 这是我第一次决定只使用该路径来创建文件,我欠你一条命。我欠你一杯啤酒,谢谢。
    猜你喜欢
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 2020-11-27
    • 2011-10-14
    相关资源
    最近更新 更多