Android 如何从推送通知单击打开图像?
在我的应用程序中,我可以选择捕获屏幕截图,并将其保存在下面的自定义目录中(如/storage/emulated/0/MyApp/screenshots/image.jpeg) 因此,我想应用程序Android 如何从推送通知单击打开图像?,android,push-notification,notifications,android-notifications,Android,Push Notification,Notifications,Android Notifications,在我的应用程序中,我可以选择捕获屏幕截图,并将其保存在下面的自定义目录中(如/storage/emulated/0/MyApp/screenshots/image.jpeg) 因此,我想应用程序Pending Intent,用户可以单击通知,屏幕截图将在某个默认查看器上打开 问题是:如何配置挂起的意图,以便在用户单击通知图像后,在某个默认查看器中打开该图像 例如:我有一个像素手机,可以选择制作屏幕截图,截图准备好后,我可以点击并打开图像…最终我找到了这篇文章 Intent intent = n
Pending Intent
,用户可以单击通知,屏幕截图将在某个默认查看器上打开
问题是:如何配置挂起的意图,以便在用户单击通知图像后,在某个默认查看器中打开该图像
例如:我有一个像素手机,可以选择制作屏幕截图,截图准备好后,我可以点击并打开图像…最终我找到了这篇文章
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>
代码更改
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);
我得到这样的异常
android.os.FileUriExposedException:file:///storage/emulated/0/MyApp/screenshots/image.jpeg 通过Intent.getData()在应用程序之外公开。
file:///替换为文件://
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);