0

I have a application where I have some downloaded images in a folder in SD Card. I want to save it as a wallpaper.

using the below code user can set it as wallpaper.

WallpaperManager myWallpaperManager = WallpaperManager.getInstance(context);
myWallpaperManager.setBitmap(loadedImage);

However this does not bring up any UI for user to select a part of the image like crop operation when selecting a image from Gallery app to set wallpaper. I would like my code to trigger such a operation. When users click a button in my app I want to take them to gallery app with crop option to set the wallpaper.

Please let me know how to do this. Thank you.

Jagger
  • 10,350
  • 9
  • 51
  • 93
Vinod
  • 31,933
  • 35
  • 96
  • 119
  • which Android version do you target? – Shinigamae Dec 03 '12 at 03:28
  • Hi @Shinigamae: I want to support from 2.1. But if it is only possible from a particular version, I am okay with it. – Vinod Dec 03 '12 at 03:35
  • I did have a simple project, on 2.3. I allow user to choose an image from his library (saved from a website before) then allow him to crop that image. But I found out it didn't work well on Android 3.0 and 4.0. Need some work-around then. – Shinigamae Dec 03 '12 at 03:38

1 Answers1

1

You may want to try this:

  1. To select from your library (SD Card included) - void selectPhoto():

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Choose photo to upload"), PICK_FROM_FILE);
    
  2. To start the crop action - void doCrop():

    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");
    
    // Check if there is image cropper application installed.
    List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
    
    int size = list.size();
    
    // If no cropper application is found, throw a message.
    if (size == 0) {            
        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
        return;
    
    // If there're cropper applications found, use the first
    } else {
    
        // Specify image path and cropping parameters
        intent.setData(mImageCaptureUri);
        intent.putExtra("outputX", 0);
        intent.putExtra("outputY", 0);
        intent.putExtra("return-data", true);
    
        Intent i = new Intent(intent);
        ResolveInfo res = list.get(0);
        i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        startActivityForResult(i, CROPPED_IMAGE);
    
  3. Handle Activity results - void onActivityResult(int requestCode, int resultCode, Intent data)

    if (resultCode != RESULT_OK) return;
    
    switch (requestCode) {
        case PICK_FROM_FILE: 
            mImageCaptureUri = data.getData();
            doCrop();
            break;          
        case CROPPED_IMAGE:         
            Bundle extras = data.getExtras();
            try{
                if (extras != null) {
                     Bitmap myImage = extras.getParcelable("data");
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            break;
    

This code will activate crop action right after you selected the image.

Note that mImageCaptureUri is the selected image URI, it would be pass to intent of cropping action.

Shinigamae
  • 854
  • 3
  • 18
  • 35