繁体   English   中英

多种设备上的Android壁纸

[英]Android wallpaper on multiple devices

我想从画廊设置墙纸。 选定的图像将必须使用设备的CropImage类。

问题在于每个设备都有一个不同的CropImage类,因此当我使用“ Crop”操作时,设备的CropImage会打开,但不会全部设置为墙纸。

码:

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

对于某些设备,它根本不设置为墙纸(如HTC)。 可能还需要设置其他一些内容,例如“设置为墙纸”和“设置为墙纸” ...

是否有通用方法为所有设备设置带有裁切器的墙纸?

在某些手机(例如HTC)上,这势必不起作用,因为它们使用自己的图库/相机。 对于这些设备,您可以仅使用未裁剪的图像作为单独的资源。 但是,如果您具有图像的位图,则将添加此功能以设置为墙纸:

  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();
    }         
}

您应该为此添加权限

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

这是从这里拿来的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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