安卓集图像作为联系人图标/壁纸图标、图像、联系人、壁纸

2023-09-12 22:06:39 作者:再见再见、再也不见

我写我自己的图像浏览,现在我想有设置为功能,例如在Android原生图像浏览。我现在是可能的,因为Facebook拥有它。我已经附加了截图,以让自己更清楚。

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。我想给什么不顺心更详细的解释。当我在菜单中选择联系人图标出现的我的联系人列表中。当我选择一个联系人施加力关闭。如果我选择主页/锁屏壁纸它打开我的手机的画廊。 这是我的code片断:

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

我还添加了随之而来的权限,以我的表现,我能写我的形象到手机的SD卡。

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.

推荐答案

从谷歌库code(URL应该在某处的 http://android.git.kernel.org/ 但它的下跌。在写的时候 - 我会尽力和更新的链接时,它的备份):

From the Google Gallery code (URL should be somewhere in http://android.git.kernel.org/ but it's down at time of writing - I'll try and update the link when it's back up):

// 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

From 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;
}