当前位置: 首页 > 知识库问答 >
问题:

如何在Android中读取存储文件?

费学
2023-03-14

我正在保存一个excel文件到设备(Android7)的存储,现在我想当用户点击按钮时打开excel文件,但现在当按钮点击应用程序会崩溃,而当im到我的存储和im打开文件直接在我的应用程序之外没有问题!!如果我的代码行错了,请帮忙,谢谢

日志:Android.os.FileUriExposedException:file:///storage/emulated/0/MessangerApp/MessangerDocuments/139703251134.xlsx通过Android.os.StrictMode.OnFileUriExposed的Intent.getData()向应用程序以外公开

文件目录:内部存储>MessangerApp>MessangerDoucements>Test.xlsx

这里是我的代码:

   File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + Config.DOC_DIRECTORY_Name + filename);
            Uri path = Uri.fromFile(file);

            Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
            pdfOpenintent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            pdfOpenintent.setDataAndType(path, "application/vnd.ms-excel");
            view.getContext().startActivity(pdfOpenintent);
}

共有1个答案

谭兴学
2023-03-14

对于Android N,为了解决这个问题,您需要使用FileProvider API。

<manifest ...>
<application ...>
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</application>

创建XML文件res/XML/provider_path.XML

<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>

在外部office应用程序中打开文件

File excelFile = new File(Environment.getExternalStorageDirectory(),"nameexcelFile.pdf");//File path
        if (excelFile.exists()) //Checking if the file exists or not
        {
            Uri path = Uri.fromFile(excelFile);
            Intent objIntent = new Intent(Intent.ACTION_VIEW);
            objIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(objIntent);//Starting the excel file viewer
        } else {

            Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show();

        }
 类似资料:
  • 问题内容: 我需要在存储过程中读入由SSIS包创建的日志文件。 有什么好方法吗? 我尝试使用此代码,但将文件内容显示为乱码。有没有解决的编码问题?有没有更简单的方法? 问题答案: 您是否尝试过将笔直插入?例如:

  • 本文向大家介绍Android学习之文件存储读取,包括了Android学习之文件存储读取的使用技巧和注意事项,需要的朋友参考一下 前言 相信大家都知道知道,在AndroidOS中,提供了五中数据存储方式,分别是:ContentProvider存储、文件存储、SharedPreference存储、SQLite数据库存储、网络存储。那么这一篇,我们介绍文件存储。 1.Android文件的操作模式 学过J

  • 我想从文本文件中读取文本。在下面的代码中,会发生异常(这意味着它会转到catch块)。我将文本文件放在应用程序文件夹中。我应该把这个文本文件(mani.txt)放在哪里才能正确阅读?

  • 我有一个map-reduce作业,并且reducer获得一个驻留在Azure Blob存储中的文件的绝对地址,并且reducer应该打开它并读取它的内容。在配置Hadoop集群(HDInsight)时,我添加了包含文件的存储帐户。因此,还原器必须有权访问这个Blob存储,但Blob存储不是我的作业的默认HDFS存储。我的reducer中有以下代码,但它给了我一个FileNotFound错误消息。

  • 如何从包含大量数字的文本文件中读取并将其存储到int数组中,最后将该数组和数组大小传递给应用程序中使用的其他方法。到目前为止,我的代码读取文本文件并将其存储到ArrayList中,而不是数组中。谢谢你。

  • 问题内容: 我有一个文件,其内容为python列表的形式,如下所示: 有什么办法可以将python文件读回到列表对象中吗?而不是使用整个文件,而是将其读取为字符串。 编辑:对于那些可能有兴趣的人,我使用(import ast)遇到了一个奇怪的问题,作为解决上述问题的建议。 我在其中使用的程序具有从yahoo finance python模块获取历史股票数据的功能。此函数与ast.literal_e