0

我想在点击通知时打开下载的文件。当使用 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;
        }
    }
4

0 回答 0