0

I would like to set a wallpaper from the gallery. The selected image will have to use the CropImage class of the device.

The problem is that every device have a different CropImage class, so when im using "Crop" action, the CropImage of the device opens, but not all set as a wallper.

Code:

Intent cropperIntent = new Intent("com.android.camera.action.CROP", chosenImageUri);
        cropperIntent.setDataAndType(chosenImageUri, "image/*");

        cropperIntent.putExtra("crop", true);
        cropperIntent.putExtra("aspectX", outSize.x);
        cropperIntent.putExtra("aspectY", outSize.y);
        cropperIntent.putExtra("outputX", outSize.x);
        cropperIntent.putExtra("outputY", outSize.y);
        cropperIntent.putExtra("width", outSize.x);
        cropperIntent.putExtra("height", outSize.y);
        cropperIntent.putExtra("scale", true);
        cropperIntent.putExtra("noFaceDetection", true);
        cropperIntent.putExtra("set-as-wallpaper", true); // for: com.android.gallery3d.app.CropImage
        cropperIntent.putExtra("setWallpaper", true); // for: com.android.camera.CropImage

For some devices its not set as wallpaper at all (Like HTC). Maybe have to set another extra like the "set-as-wallpaper" and "set-as-wallpaper"...

Is there a generic method to set the wallpaper with it's cropper for all devices?

j0k
  • 22,600
  • 28
  • 79
  • 90
Ofir
  • 1

1 Answers1

0

This is bound to not work on some phones, like HTC because they use their own gallery/camera. For those devices, you can just use an uncropped image, as a separate resource. However, if you have bitmap of image than you will add this function to set as wallpaper:

  public void SetBackground(int Url) {

    try {
        File file = new File("/sdcard/sampleimage");
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Url);
        bitmap.compress(CompressFormat.JPEG, 80, new FileOutputStream(file));
        Context context = this.getBaseContext();
        context.setWallpaper(bitmap);            
        Toast.makeText(getApplicationContext(), "Wallpaper has been set",             Toast.LENGTH_SHORT).show();            
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }         
}

you should add permission for this

<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>

This is taken from here.

Community
  • 1
  • 1
g00dy
  • 6,752
  • 2
  • 30
  • 43