2

I'm using the picasso library for my app and it works very well so far. I have two activities, one which displays all the images and one that displays one image when I clicked on it in the first activity. The image is loaded into an imageView. Now I want to set the content of this imageView as my homescreen wallpaper. So far I have this:

if (id == R.id.set_wall) {


        Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.id.image);
        WallpaperManager myWallpaperManager = WallpaperManager
                .getInstance(getApplicationContext());

        try {
            myWallpaperManager.setBitmap(mBitmap);
            Toast.makeText(DetailActivity.this, "Wallpaper set",
                    Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(DetailActivity.this,
                    "Error setting wallpaper", Toast.LENGTH_SHORT)
                    .show();
        }
        return true;


    }

But this gives me this error:

 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)'  on a null object reference

Does anyone have an idea how I can set the content of this imageView as my wallpaper?

Thank you a lot!!

timbremer
  • 133
  • 2
  • 15

1 Answers1

3

replace Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.id.image);

by Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);

if you do not have a Drawable then ,

code to get bitmap from imageview

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
Orbitcoder
  • 462
  • 1
  • 3
  • 16
  • This doesn't work because I don't have the images as a drawable. As I said, I'm using picasso for loading the images from the internet. – timbremer Jan 04 '15 at 16:08
  • then you should not use BitmapFactory.decodeResource() , this is not for R.id.* – Orbitcoder Jan 04 '15 at 16:10