Sirop4ik
Sirop4ik

Reputation: 5243

How to open image from push notification click?

In my application I have a option to capture a screenshot and save it below in custom dir (like /storage/emulated/0/MyApp/screenshots/image.jpeg).

So, I want to app Pending Intent that user can click on the notification and the screenshot will be opened on some default viewer...

Question is : how to configure the pending intent such way that after user click on the notification image will be opened within some default viewer?

For example: I have a Pixel phone and there is option to make screenshot, after that screenshot is ready I can click and image will be opened...

Upvotes: 0

Views: 1250

Answers (2)

Sirop4ik
Sirop4ik

Reputation: 5243

Eventually I found this article

https://medium.com/@ali.muzaffar/what-is-android-os-fileuriexposedexception-and-what-you-can-do-about-it-70b9eb17c6d0

and here what I get

Manifest

<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>
</manifest>

Create XML file 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="."/>
</paths>

Code changes

Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);

    File file = new File(iImagePath); // set your image path

    Uri uri = FileProvider.getUriForFile(iC, iC.getApplicationContext().getPackageName() + ".provider", file);
    intent.setDataAndType(uri, "image/*");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    PendingIntent pIntent = PendingIntent.getActivity(iC, 0, intent, 0);

    Bitmap bm = BitmapFactory.decodeFile(iImagePath);

    NotificationCompat.Builder builder = mScreenshotBuilder.setContentTitle(iC.getString(R.string.screenshot_saved))//
                                                           .setContentText(iC.getString(R.string.tap_to_view_your_screenshot))//
                                                           .setContentIntent(pIntent);

Upvotes: 2

Prashant Arvind
Prashant Arvind

Reputation: 2337

 Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    File file = new File("YOUR_IMAGE"); // set your image path 
    intent.setDataAndType(Uri.fromFile(file), "image/*");

    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

    Notification noti = new NotificationCompat.Builder(this)
            .setContentTitle("Screenshot")
            .setContentText("Image Name")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent).build();

    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, noti);

Upvotes: 0

Related Questions