我已经编写了自己的ImageViewer,现在我想要像Android本机ImageViewer一样拥有“设置为”功能。 我知道这是可能的,因为Facebook有它。 为了使自己更清楚,我附加了一张屏幕截图。
附注:我想更详细地解释出现的问题。 在菜单中选择“联系人图标”后,我的联系人列表将出现。 当我选择一个联系人时,应用程序会强制关闭。 如果我选择“主屏幕/锁定屏幕壁纸”,它将打开我的手机相册。 以下是我的代码片段:
Bitmap icon = mBitmap;
Intent setAs = new Intent(Intent.ACTION_ATTACH_DATA);
setAs.setType("image/jpg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "/my_tmp_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
setAs.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/my_tmp_file.jpg"));
startActivity(Intent.createChooser(setAs, "Set Image As"));
我已经将相应的权限添加到我的清单中,并且能够将我的图像写入手机的SD卡。