将图像设为Android联系人头像/壁纸

6

我已经编写了自己的ImageViewer,现在我想要像Android本机ImageViewer一样拥有“设置为”功能。 我知道这是可能的,因为Facebook有它。 为了使自己更清楚,我附加了一张屏幕截图。 enter image description here

附注:我想更详细地解释出现的问题。 在菜单中选择“联系人图标”后,我的联系人列表将出现。 当我选择一个联系人时,应用程序会强制关闭。 如果我选择“主屏幕/锁定屏幕壁纸”,它将打开我的手机相册。 以下是我的代码片段:

                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卡。 LogCat输出

你需要发送一个意图。这很简单。你只需要搜索一些关于编辑联系人和程序化更改壁纸的代码即可。 - Sherif elKhatib
你能给我们提供一下logcat错误输出吗? - Martyn
你能将错误信息复制粘贴为文本吗? - Martyn
你指的是哪个文本?RuntimeException 还是 ActivityNotFoundException? - superM
09-05 13:48:49.465: ERROR/AndroidRuntime(730): 由于android.content.ActivityNotFoundException:找不到处理意图{act=com.android.camera.action.CROP(有额外信息)}的活动而引起。 - superM
显示剩余2条评论
5个回答

4

以下内容来自Google相册应用程序源代码

// Called when "Set as" is clicked.
private static boolean onSetAsClicked(MenuInvoker onInvoke,
                                      final Activity activity) {
    onInvoke.run(new MenuCallback() {
        public void run(Uri u, IImage image) {
            if (u == null || image == null) {
                return;
            }

            Intent intent = Util.createSetAsIntent(image);
            activity.startActivity(Intent.createChooser(intent,
                    activity.getText(R.string.setImage)));
        }
    });
    return true;
}

来自 Utils.java

// Returns an intent which is used for "set as" menu items.
public static Intent createSetAsIntent(IImage image) {
    Uri u = image.fullSizeImageUri();
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    intent.setDataAndType(u, image.getMimeType());
    intent.putExtra("mimeType", image.getMimeType());
    return intent;
}

我无法弄清楚IImage是什么。Eclipse没有给出适当的建议。 - superM
看起来这只是一个接口 - fullSizeImageUri 定义为 public abstract Uri fullSizeImageUri(); 所以它只是返回一个指向图像的 Uri - Martyn
我会尝试了解getMimeType并尝试您的代码。非常感谢。 - superM
1
我已经尝试过了,但是它不起作用。我收到一个弹出窗口显示“没有应用程序可以执行此操作”。不管怎样,还是谢谢。 - superM
这在新的Google相册应用程序中停止工作了。解决方法似乎非常笨拙。 - milosmns

3

查看联系人应用程序代码。有一个名为 AttachImage 的活动,可用于附加图像。图标照片应为96x96像素尺寸。 action ... CROP 在传递的图像上进行人脸检测和裁剪。

链接:AttachImage.java

您应该将图像缩放并裁剪为96x96,并将其URI传递给 AttachImage 活动中使用的 insertPhoto 方法。

要更改壁纸,您可以参考此问题的答案。

更新

启动裁剪活动的代码:

Intent intent = new Intent("com.android.camera.action.CROP", myIntent.getData());
if (myIntent.getStringExtra("mimeType") != null) {
   intent.setDataAndType(myIntent.getData(), myIntent.getStringExtra("mimeType"));
}
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_CROP_PHOTO);

你的需求有什么不同之处? - Ron
就我所知,这些链接可以让你将图片附加到联系人或将其设置为壁纸。此外,我想打开本地的裁剪页面,而不是通过编程裁剪图像。我希望这个功能与原生应用一样。 - superM
非常感谢,我会将它们组合起来得到我想要的结果。 - superM
1
链接AttachImage.java无效。 - Shankar Agarwal

1
使用这段代码。
File externalFile=new File("filePath");
Uri sendUri = Uri.fromFile(externalFile);
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
            intent.setDataAndType(sendUri, "image/jpg");
            intent.putExtra("mimeType", "image/jpg");
            startActivityForResult(Intent.createChooser(intent, "Set As"), 200);

1

您可以简单地使用WallpaperManager来设置壁纸。

WallpaperManager.getInstance(this).setBitmap(mBitmap);

这种方法在所有设备、分辨率和操作系统版本上都表现最佳(忽略2.0-)。但是这种方法无法进行裁剪 :/ - milosmns

0

将图像设置为(联系人头像,壁纸等)

        Intent setAs = new Intent(Intent.ACTION_ATTACH_DATA);
        setAs.setType("image/jpg");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.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.setDataAndType(Uri.parse("file:///sdcard/my_tmp_file.jpg"),
                "image/jpg");
        setAs.putExtra("mimeType", "image/jpg");
        startActivity(Intent.createChooser(setAs, "Set Image As"));

这将解决您的问题并将图像设置为(联系人、壁纸等)


我尝试实现这个方法,它成功地弹出了选择器,但是无法加载图像,有什么想法吗? - martinseal1987
嗨 @martinseal1987,请告诉我在哪个Android版本中无法加载图像... - Jay Kansagara

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接