android set image as contact icon/wallpaper
Asked Answered
T

5

6

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. enter image description here

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.

LogCat Output

Tiros answered 2/9, 2011 at 13:30 Comment(7)
you will have to send an intent. it is simple. all you have to do is search for some code on editing a contact and on changing the wallpaper programaticallySink
I can't find code which will open OptionsMenu like in native ImageViewer. And after that when I choose an action it should continue like native. It is easier to do either, but I can't do what I need.Tiros
Can you give us a logcat error output?Trollop
Ok. I've add it to my question.Tiros
Can you copy and paste the error as text?Trollop
Which text do you mean? RuntimeException or ActivityNotFoundException?Tiros
09-05 13:48:49.465: ERROR/AndroidRuntime(730): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.camera.action.CROP (has extras) }Tiros
T
4

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

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;
}
Trollop answered 5/9, 2011 at 8:56 Comment(5)
I can't figure out what is IImage. Eclipse gives no appropriate suggestions.Tiros
Looks like it's just an interface - fullSizeImageUri is defined as public abstract Uri fullSizeImageUri(); so it's just returning a Uri to the imageTrollop
I'll try to find more about getMimeType and will try your code. Thank you very much.Tiros
I've tried it but it doesn't work. I get a popup with this "No application can perform this action". but thanks anyway.Tiros
This stopped working with new Google Photos app. Workaround seems to be really tackyWisnicki
B
3

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. The action...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 in AttachImage activity.

For changing wallpaper you can refer this question's answer.

Update

Code for launching cropping activity:

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);
Blowfly answered 6/9, 2011 at 12:13 Comment(4)
What is the difference in your requirementBlowfly
as far as I could get these link allow either to attach image to contact or to set it as a wallpaper. Besides I want to open the native cropping page and not to crop the image programmatically. I want this to work just like the native.Tiros
Thank you very much, I'll to combine these all and get the result I want.Tiros
the link AttachImage.java is not valid.Superficies
A
1

You can simply use WallpaperManager to set the wallpaper.

WallpaperManager.getInstance(this).setBitmap(mBitmap);
Artamas answered 10/9, 2011 at 19:41 Comment(1)
This works best on all devices, resolutions and OS versions (ignoring 2.0-) No cropping available with this approach though :/Wisnicki
C
1

use this code

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);
Counterpressure answered 1/7, 2015 at 10:50 Comment(0)
K
0

For Set image as (Contact,wallpaper,etc.)

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

This will solve your problem and set the image as (Contact,Wallpaper,etc..)

Kazan answered 16/4, 2015 at 9:55 Comment(2)
i have tried to implement this method it works on bringing up the chooser but then fails to load the image, any ideas?Kosaka
hi @Kosaka in which android version fails to load image please describe me....Kazan

© 2022 - 2024 — McMap. All rights reserved.