android 将图像设置为联系人图标/壁纸
我已经编写了自己的 ImageViewer,现在我想要像 Android 原生 ImageViewer 一样具有设置为功能。我现在这是可能的,因为 Facebook 已经有了它。我附上了屏幕截图以使自己更清楚。
PS 我想对出现的问题提供更详细的解释。在菜单中选择“联系人图标”后,会出现我的联系人列表。当我选择一个联系人时,应用程序强制关闭。如果我选择“主屏幕/锁屏壁纸”,它会打开我手机的图库。 这是我的代码片段:
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 卡。
I have written my own ImageViewer and now I want to have Set as functionality like in Android native ImageViewer. I now it is possible since Facebook has it. I've attached a screenshot to make myself more clear.
P.S. I want to give a more detailed explanation of what goes wrong. After I choose "Contact icon" in the menu the list of my contacts appears. When I choose a contact the application force closes. If I choose "Home/Lock screen wallpaper" it opens my phone's gallery.
Here is my code snippet:
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"));
I have also added the consequent permissions to my manifest and I am able to write my image to the sd card of the phone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自 Google 图库应用源代码:
来自 Utils.java
From the Google Gallery app source code:
From Utils.java
查看联系人应用程序代码。有一个
AttachImage
活动启动用于附加图像。图标照片的尺寸应为 96x96 像素。action...CROP
对您传递的图像进行人脸检测和裁剪。链接:AttachImage.java
应将图像缩放并裁剪为 96x96,并将其 URI 传递给
AttachImage
中使用的insertPhoto
方法活动。要更改壁纸,您可以参考此问题的答案。
更新
启动裁剪活动的代码:
Take a look at the contacts app code. There is an
AttachImage
activity that launches for attaching an image. The icon photo should be 96x96 px dimension. Theaction...CROP
does face detection and cropping on the image you pass.Link : AttachImage.java
You should scale and crop the image to 96x96 and pass its URI to the
insertPhoto
method used inAttachImage
activity.For changing wallpaper you can refer this question's answer.
Update
Code for launching cropping activity:
您可以简单地使用 WallpaperManager 来设置壁纸。
You can simply use WallpaperManager to set the wallpaper.
使用此代码
use this code
对于将图像设置为(联系人、壁纸等)
这将解决您的问题并将图像设置为(联系人、壁纸等..)
For Set image as (Contact,wallpaper,etc.)
This will solve your problem and set the image as (Contact,Wallpaper,etc..)