如何从推送通知点击打开图像?
How to open image from push notification click?
在我的应用程序中,我可以选择捕获屏幕截图并将其保存在自定义目录中(例如 /storage/emulated/0/MyApp/screenshots/image.jpeg)。
所以,我想应用 Pending Intent
用户可以点击通知,屏幕截图将在某些默认查看器上打开...
问题是:如何配置待定意图,以便在用户单击通知图像后在某些默认查看器中打开?
例如:我有一个 Pixel phone 并且有制作屏幕截图的选项,在屏幕截图准备好后我可以点击并打开图像...
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);
终于找到了这篇文章
这是我得到的
清单
<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>
创建XML 文件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>
代码更改
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);
在我的应用程序中,我可以选择捕获屏幕截图并将其保存在自定义目录中(例如 /storage/emulated/0/MyApp/screenshots/image.jpeg)。
所以,我想应用 Pending Intent
用户可以点击通知,屏幕截图将在某些默认查看器上打开...
问题是:如何配置待定意图,以便在用户单击通知图像后在某些默认查看器中打开?
例如:我有一个 Pixel phone 并且有制作屏幕截图的选项,在屏幕截图准备好后我可以点击并打开图像...
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);
终于找到了这篇文章
这是我得到的
清单
<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>
创建XML 文件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>
代码更改
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);