【问题标题】:What does it mean for a method to be deprecated, and how can I resolve resulting errors?不推荐使用方法意味着什么,如何解决由此产生的错误?
【发布时间】:2017-08-23 19:06:46
【问题描述】:
为什么我会在包含 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;
【问题讨论】:
标签:
java
android
android-wallpaper
【解决方案1】:
当某些东西被弃用时,这意味着开发人员已经创建了一种更好的方法,并且您不应该再使用旧的或弃用的方法。不推荐使用的内容将来可能会被删除。
在您的情况下,如果您有图像路径,设置壁纸的正确方法如下:
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);
如果您有图像 URI,请使用以下内容:
wallpaperManager = WallpaperManager.getInstance(this);
wallpaperDrawable = wallpaperManager.getDrawable();
mImageView.setImageURI(imagepath);
来自 Maidul 对this 问题的回答。
【解决方案2】:
“已弃用”表示您正在使用的特定代码不再是实现该功能的推荐方法。您应该查看给定方法的文档,它很可能会在相应位置提供指向推荐方法的链接。
【解决方案3】:
WallpaperManager myWallpaperManager=WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(bmp);
}
catch (IOException e) {
Toast.makeText(YourActivity.this,
"Ooops, couldn't set the wallpaper",
Toast.LENGTH_LONG).show();
}