What does it mean for a method to be deprecated, and how can I resolve resulting errors?
Asked Answered
S

4

6

Why do I get a deprecation error on the line containing setWallpaper(bmp), and how can I resolve it?

Error: The method setWallpaper(Bitmap) from the type Context is deprecated

switch(v.getId()){
 case R.id.bSetWallpaper:
try {
            getApplicationContext().setWallpaper(bmp);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;
Sclera answered 3/3, 2013 at 19:58 Comment(3)
I think you've made a mistake in your question. Can you clarify the error for us please?Nairobi
Yeah sorry I have edited the post, missed it out by accident.Sclera
"deprecated" means you shouldn't be using it.Babin
K
11

When something is deprecated, it means the developers have created a better way of doing it and that you should no longer be using the old, or deprecated way. Things that are deprecated are subject to removal in the future.

In your case, the correct way to set the wallpaper if you have an image path is as follows:

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

If you have an image URI, then use the following:

wallpaperManager = WallpaperManager.getInstance(this);
wallpaperDrawable = wallpaperManager.getDrawable();
mImageView.setImageURI(imagepath);

From Maidul's answer to this question.

Keep answered 3/3, 2013 at 20:2 Comment(1)
Thanks I'll look into trying this when I get back to the code.Sclera
N
5

"Deprecated" means that the particular code you are using is no longer the recommended method of achieving that functionality. You should look at the documentation for your given method, and it will more than likely provide a link to the recommended method in it's place.

Nairobi answered 3/3, 2013 at 20:2 Comment(0)
M
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();
}
Mike answered 18/3, 2013 at 1:42 Comment(0)
R
1

You should use WallpaperManager.setStream() instead of Context.setWallpaper() as it is deprecated and may be removed in new API releases.

Rockoon answered 3/3, 2013 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.