【问题标题】:android set image as contact icon/wallpaperandroid将图像设置为联系人图标/壁纸
【发布时间】:2011-11-09 04:38:52
【问题描述】:

我已经编写了自己的 ImageViewer,现在我想拥有像 Android 原生 ImageViewer 中的 Set as 功能。我现在有可能,因为 Facebook 有它。我附上了一个截图,让自己更清楚。

附:我想更详细地解释哪里出了问题。在我在菜单中选择“联系人图标”后,我的联系人列表就会出现。当我选择一个接触时,应用力关闭。如果我选择“主页/锁屏壁纸”,它会打开我手机的图库。 这是我的代码 sn-p:

                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 卡。

【问题讨论】:

  • 你必须发送一个意图。很简单。您所要做的就是搜索一些有关编辑联系人和以编程方式更改壁纸的代码
  • 我找不到可以像在本机 ImageViewer 中一样打开 OptionsMenu 的代码。之后,当我选择一个动作时,它应该像原生一样继续。两者都比较容易做,但我不能做我需要的。
  • 你能给我们一个logcat错误输出吗?
  • 好的。我已将其添加到我的问题中。
  • 你能把错误复制粘贴为文本吗?

标签: android image viewer


【解决方案1】:

来自Google Gallery app source code

// 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 没有给出合适的建议。
  • 看起来它只是一个接口 - fullSizeImageUri 被定义为 public abstract Uri fullSizeImageUri(); 所以它只是返回一个 Uri 到图像
  • 我将尝试查找有关 getMimeType 的更多信息并尝试您的代码。非常感谢。
  • 我试过了,但是不行。我收到一个弹出窗口,显示“没有应用程序可以执行此操作”。不过还是谢谢。
  • 这停止使用新的 Google 相册应用。解决方法似乎很俗气
【解决方案2】:

查看联系人应用代码。有一个 AttachImage 活动启动以附加图像。图标照片的尺寸应为 96x96 像素。 action...CROP 对您传递的图像进行面部检测和裁剪。

链接:AttachImage.java

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

如需更换壁纸,您可以参考question 的答案。

更新

启动裁剪活动的代码:

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);

【讨论】:

  • 你的要求有什么不同
  • 据我所知,这些链接允许将图像附加到联系人或将其设置为墙纸。此外,我想打开本机裁剪页面,而不是以编程方式裁剪图像。我希望它可以像本地人一样工作。
  • 非常感谢,我会把这些结合起来,得到我想要的结果。
  • 链接 AttachImage.java 无效。
【解决方案3】:

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

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

【讨论】:

  • 这在所有设备、分辨率和操作系统版本上效果最佳(忽略 2.0-)但是这种方法无法进行裁剪:/
【解决方案4】:

用于将图片设置为(联系人、壁纸等)

        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"));

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

【讨论】:

  • 我试图实现这个方法,它可以调出选择器,但无法加载图像,有什么想法吗?
  • 嗨@mar​​tinseal1987 android 版本无法加载图像请描述我....
【解决方案5】:

使用此代码

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    相关资源
    最近更新 更多