6

我已经编写了自己的 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 卡。

LogCat 输出

4

5 回答 5

4

来自Google Gallery 应用程序源代码

// 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;
}
于 2011-09-05T08:56:51.333 回答
3

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

链接:AttachImage.java

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

对于更换壁纸,您可以参考这个问题的答案。

更新

启动裁剪活动的代码:

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);
于 2011-09-06T12:13:33.500 回答
1

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

WallpaperManager.getInstance(this).setBitmap(mBitmap);
于 2011-09-10T19:41:29.600 回答
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);
于 2015-07-01T10:50:32.890 回答
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"));

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

于 2015-04-16T09:55:00.827 回答