0

I want to open the downloaded file on click of notification. It was working fine when content intent was set using fileUri. But in android N due to enhanced file security it is giving FileUriExposedException. But now after content uri is used , notification click just opens the file location not the file. Below is the code.

@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;
        }
    }
Jaggs
  • 75
  • 2
  • 6

0 Answers0