如何从 android 中的存储读取文件?
How to read file from storage in android?
我正在将 excel 文件保存到设备的 存储 (Android 7) ,现在我想当用户点击按钮时 excel 文件被打开但是现在当点击按钮时应用程序会崩溃而当我转到我的存储并直接在我的应用程序外部打开文件没有问题!如果我在任何代码行中有错误,请帮助,谢谢
Log : android.os.FileUriExposedException: file:///storage/emulated/0/MessangerApp/MessangerDocuments/139703251134.xlsx
exposed beyond app through Intent.getData()
at android.os.StrictMode.onFileUriExposed
Directory of file : internal storage > MessangerApp >
MessangerDoucments >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);
}
自 AndroidN 起,为了解决此问题,您需要使用 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_paths.xml
<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
在外部办公室应用程序中打开文件
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();
}
我正在将 excel 文件保存到设备的 存储 (Android 7) ,现在我想当用户点击按钮时 excel 文件被打开但是现在当点击按钮时应用程序会崩溃而当我转到我的存储并直接在我的应用程序外部打开文件没有问题!如果我在任何代码行中有错误,请帮助,谢谢
Log : android.os.FileUriExposedException: file:///storage/emulated/0/MessangerApp/MessangerDocuments/139703251134.xlsx exposed beyond app through Intent.getData() at android.os.StrictMode.onFileUriExposed
Directory of file : internal storage > MessangerApp > MessangerDoucments >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);
}
自 AndroidN 起,为了解决此问题,您需要使用 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_paths.xml
<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
在外部办公室应用程序中打开文件
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();
}