我想点击通知打开下载的文件。使用fileUri设置内容意图时工作正常。但是在android N中由于增强了文件安全性,它给出了FileUriExposedException。但是现在使用内容uri后,通知单击只打开文件位置而不是文件。 以下是代码。
@Override
protected void onPostExecute(String filePath) {
super.onPostExecute(filePath);
if (fileNameAndExtn != null && !fileNameAndExtn.equalsIgnoreCase("")) {
Toast.makeText(mContext, fileNameAndExtn + " downloaded successfully", Toast.LENGTH_LONG).show();
mBuilder.setProgress(100, 100, false);
Intent intent = getPendingIntent(mContext, filePath);
mBuilder.setContentTitle(fileNameAndExtn)
.setAutoCancel(true)
.setContentText("Download complete");
PendingIntent pendingIntent = null;
if (intent != null) {
pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
}
if (pendingIntent != null) {
mBuilder.setContentIntent(pendingIntent).setProgress(0, 0, false);
}
mNotifyManager.notify(mUniqueId, mBuilder.build());
}
}
public Intent getPendingIntent(Context context, String filePath) {
File file = new File(filePath);
Uri contentUri= FileProvider.getUriForFile(context, AppConstants.FILE_PROVIDER_AUTHORITY,file);
Intent intent = new Intent(Intent.ACTION_VIEW,contentUri);
intent.setType(getMimeType(filePath));
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (intent.resolveActivityInfo(context.getApplicationContext().
getPackageManager(), 0) != null) {
return intent;
} else {
Toast.makeText(context, "File not present or Explorer app not present.",
Toast.LENGTH_LONG).show();
return null;
}
}