BÀI 66 - MỞ FILE EXCEL BẰNG INTENT TRONG ANDROID

  • Trong Mainifest thêm thẻ Provider trong Application

<provider

android:name="androidx.core.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>

  • Trong thư mục res, tạo file xml với tên "provider_paths"

<?xml version="1.0" encoding="utf-8"?>

<paths>

<external-path name="external_files" path="."/>

</paths>

  • Code intent như sau:

private void ActionOpenFileExcel() {

Intent openExcel = new Intent(Intent.ACTION_VIEW);

Uri uri = FileProvider.getUriForFile(getApplicationContext(),

getApplicationContext().getPackageName() + ".provider",

filePath);

openExcel.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

openExcel.setDataAndType(uri, "application/vnd.ms-excel");

try {

startActivity(openExcel);

} catch (Exception e) {

Log.e("excel", String.valueOf(e));

}

}