简体   繁体   中英

Android wallpaper on multiple devices

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM