0

I am getting photo with my camera(mobile) and then I need that to set it wallpaper but get me crash. When I use from setWallpaper() it say me The method setWallpaper(Bitmap) from the type Context is deprecated . Here is my code :

public class Camera extends Activity implements View.OnClickListener {
    ImageButton ib;
    Button b;
    ImageView iv;
    Intent i;
    final static int cameraData = 0;
    Bitmap bmp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo);
        initialize();
    }

    private void initialize() {
        iv = (ImageView) findViewById(R.id.IVReturnedPic);
        ib = (ImageButton) findViewById(R.id.IBTakePic);
        b = (Button) findViewById(R.id.btnSetWall);
        b.setOnClickListener(this);
        ib.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnSetWall:
            try {
                getApplicationContext().setWallpaper(bmp);
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        case R.id.IBTakePic:
            i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, cameraData);
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            bmp = (Bitmap) extras.get("data");
            iv.setImageBitmap(bmp);
        }
    }
}

It get me crash from here setWallpaper():

getApplicationContext().setWallpaper(bmp);

Notice : In this code I am using View.OnClickListener .

A.A
  • 1,138
  • 1
  • 16
  • 29

4 Answers4

1

Try this way,hope this will help you to solve your problem.

Declare WallpaperManager object at class level :

private WallpaperManager wallpaperManager;

Initialize WallpaperManager object in initialize() :

private void initialize() {
  wallpaperManager = WallpaperManager.getInstance(this);
}

Set bitmap to wallpaperManager object.

case R.id.btnSetWall:
   try {
       if(bmp!=null){
           wallpaperManager.setBitmap(bmp);
       }else{ 
           // write your bitmap null handle code here.
       }
   } catch (IOException e) {
     Log.e(TAG, "Cannot set image as wallpaper", e);
   }
   break;

Add this permission to AndroidManifest.xml :

<uses-permission android:name="android.permission.SET_WALLPAPER" />
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • I can take pic but when i click on the button for set on wallpaper it get me crash . And when I see my screen mobile I see the pic that set it . It just get me crash when I click on button . – A.A Nov 04 '14 at 13:09
  • I can take pic but when i click on the button for set on wallpaper it get me crash . And when I see my screen mobile I see the pic that set it . It just get me crash when I click on button . – A.A Nov 04 '14 at 13:10
  • try to debug when click to set wallpaper button otherwise code is okay for set wallpaper. – Haresh Chhelana Nov 04 '14 at 13:13
0

I got the same problems in my android apps while setting a textarea text..... but i was able to fixed it by having an object reference in OnCreate method!!!!!! and then by using that global reference i was able to set the TextArea text property you have to look similar kind of solution...

0

You need to use the WallpaperManager Class if You develop higher API Level 5:

WallpaperManager mManager = WallpaperManager.getInstance(getApplicationContext());

  try {
      mManager.setResource(R.drawable.yourDrawable);
  } catch (IOException e) {

   //warning for the user
e.printStackTrace();

   }

And to use the manager, You need to set permissions SET_WALLPAPER in the manifest. Also, if You develop under API Level 5 and You want to use the method that You used, You have to set the Permission too.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
0

Have you added the wallpaper permission to your manifest?

 <uses-permission android:name="android.permission.SET_WALLPAPER" />
Rhynoboy2009
  • 140
  • 3
  • 11