0

I am facing a problem when i want to set bitmap as wallpape... This below code works sometime but its crashes sometime....its really strange

fabSetWallpaper.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String url = list.get(position).getImageURL();
                    ImageView img=new ImageView(SecondActivity.this);
                    Picasso.get().load(url).into(img);
                    Bitmap bitmapImg = ((BitmapDrawable) img.getDrawable()).getBitmap();

                    WallpaperManager myWallpaperManager = WallpaperManager
                            .getInstance(getApplicationContext());
                    try {
                        myWallpaperManager
                                .setBitmap(bitmapImg);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    Toast.makeText(SecondActivity.this, "Wallpaper Successfully Set On Screen ", Toast.LENGTH_LONG).show();
                }
            });

I got this exception when the app crashes

java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference

Please help i'm totally stuck there

LastKhan
  • 11
  • 1

1 Answers1

0

use dependency

implementation 'com.squareup.picasso:picasso:2.71828'

and then in your Activity onCreate()

   String url = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTe5cLRyavEKkYIBR5DnjHX4eRIb61XiIHbgMh7DU48S6Im-ZIt";


  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ImageView iv = findViewById(R.id.image_view); // if you have this in your xml

    Picasso.get()
            .load(url)
            .resize(50, 50)
            .centerCrop()
            .into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                   // iv.setImageBitmap(bitmap);


                    WallpaperManager myWallpaperManager = WallpaperManager
                            .getInstance(getApplicationContext());
                    try {
                        myWallpaperManager
                                .setBitmap(bitmap);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                @Override
                public void onBitmapFailed(Exception e, Drawable errorDrawable) {
                    Log.d("SIMPLE_CODER","fail");

                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {
                    Log.d("SIMPLE_CODER","onPrepareLoad");
                }
            });
}

In your AndroidManifest.xml dont forget to add below permission

<uses-permission android:name="android.permission.INTERNET" /> // only when you are interacting over the internet
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
SimpleCoder
  • 1,665
  • 1
  • 21
  • 34
  • i have implement this and debug it when the loadImage method calls the pointer not entering to mTarget=new Target(){.....} – LastKhan Aug 09 '18 at 03:25
  • mTarget = new Target() { . //null – LastKhan Aug 09 '18 at 03:40
  • Ideally this should work, Still you can check https://stackoverflow.com/questions/20181491/use-picasso-to-get-a-callback-with-a-bitmap for picasso usage. – SimpleCoder Aug 09 '18 at 04:03
  • @LastKhan Please check if you are passing proper url for the image in picasso. – SimpleCoder Aug 09 '18 at 04:04
  • yes passing proper url ......... i have change the String url to final...is this will be the reason? I set the String Url to final because in Picasso .load(url) here url was force me to set final with String Url... void loadImage(Context context, final String url) {........} Picasso.get() .load(url) .into(mTarget); – LastKhan Aug 09 '18 at 07:06
  • I have verified the above code and its working. Please check – SimpleCoder Aug 09 '18 at 07:24
  • Oh my mistake sorry....Picasso.get() .load(url) .into(mTarget); i write this line inside OnPrepareLoad......now it works pretty well ....thanks for your answer and time – LastKhan Aug 09 '18 at 07:33