Skip to content Skip to sidebar Skip to footer

Android Set Image As Contact Icon/wallpaper

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 screen

Solution 1:

From the Google Gallery app source code:

// Called when "Set as" is clicked.privatestaticbooleanonSetAsClicked(MenuInvoker onInvoke,
                                      final Activity activity) {
    onInvoke.run(newMenuCallback() {
        publicvoidrun(Uri u, IImage image) {
            if (u == null || image == null) {
                return;
            }

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

From Utils.java

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

Solution 2:

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:

Intentintent=newIntent("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);

Solution 3:

You can simply use WallpaperManager to set the wallpaper.

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

Solution 4:

use this code

File externalFile=newFile("filePath");
UrisendUri= Uri.fromFile(externalFile);
    Intentintent=newIntent(Intent.ACTION_ATTACH_DATA);
            intent.setDataAndType(sendUri, "image/jpg");
            intent.putExtra("mimeType", "image/jpg");
            startActivityForResult(Intent.createChooser(intent, "Set As"), 200);

Solution 5:

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

IntentsetAs=newIntent(Intent.ACTION_ATTACH_DATA);
        setAs.setType("image/jpg");
        ByteArrayOutputStreambytes=newByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        Filef=newFile(Environment.getExternalStorageDirectory()
                + File.separator + "/my_tmp_file.jpg");
        try {
            f.createNewFile();
            FileOutputStreamfo=newFileOutputStream(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..)

Post a Comment for "Android Set Image As Contact Icon/wallpaper"