对于不推荐使用的方法意味着什么,以及如何解决产生的错误?

TAM*_*TAM 6 java android android-wallpaper

为什么我在包含的行上会出现弃用错误setWallpaper(bmp),如何解决?

错误:不推荐使用Context类型的方法setWallpaper(Bitmap)

switch(v.getId()){
 case R.id.bSetWallpaper:
try {
            getApplicationContext().setWallpaper(bmp);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;
Run Code Online (Sandbox Code Playgroud)

Zac*_*tta 11

当某些东西被弃用时,这意味着开发人员已经创建了一种更好的方法,并且您不应再使用旧的或弃用的方式.被弃用的东西将来会被删除.

在您的情况下,如果您有图像路径,设置壁纸的正确方法如下:

is = new FileInputStream(new File(imagePath));
bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
Bitmap useThisBitmap = Bitmap.createScaledBitmap(
    bitmap, parent.getWidth(), parent.getHeight(), true);
bitmap.recycle();
if(imagePath!=null){
    System.out.println("Hi I am try to open Bit map");
    wallpaperManager = WallpaperManager.getInstance(this);
    wallpaperDrawable = wallpaperManager.getDrawable();
    wallpaperManager.setBitmap(useThisBitmap);
Run Code Online (Sandbox Code Playgroud)

如果您有图像URI,请使用以下内容:

wallpaperManager = WallpaperManager.getInstance(this);
wallpaperDrawable = wallpaperManager.getDrawable();
mImageView.setImageURI(imagepath);
Run Code Online (Sandbox Code Playgroud)

从Maidul对这个问题的回答.


chr*_*her 5

"不推荐使用"表示您使用的特定代码不再是实现该功能的推荐方法.您应该查看给定方法的文档,并且它很可能会提供指向它的推荐方法的链接.