Search code examples
javaandroidpicasso

How to load image from Picasso library and set it as wallpaper


I received a picture from the Internet with the Picasso Library. Then I set it up on the wallpaper. But after running on my mobile, I approached the Force close. Please help me if you make a mistake in coding or please offer if suggested for work.

  Bitmap result= null;
    try {
        result = Picasso.with(getApplicationContext())
                .load("http://wallpaperswide.com/download/the_amazing_spider_man_2012_film-wallpaper-1600x900.jpg")
                .get();
    } catch (IOException e) {
        e.printStackTrace();
    }

    WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
    try {
        wallpaperManager.setBitmap(result);
    } catch (IOException ex) {
        ex.printStackTrace();
    }

Solution

  • Suppose that you are inside MainActivity:

    package com.example.myapplication;
    
    import android.app.WallpaperManager;
    import android.graphics.Bitmap;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.widget.Toast;
    
    import com.squareup.picasso.Picasso;
    import com.squareup.picasso.Target;
    
    import java.io.IOException;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Picasso.with(this).load("http://wallpaperswide.com/download/the_amazing_spider_man_2012_film-wallpaper-1600x900.jpg").into(new Target(){
    
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
                    try {
                        wallpaperManager.setBitmap(bitmap);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    Log.d("TAG", "onBitmapLoaded: ");
                    Toast.makeText(MainActivity.this, "Wallpaper Changed", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onBitmapFailed(final Drawable errorDrawable) {
                    Log.d("TAG", "FAILED");
                    Toast.makeText(MainActivity.this, "Loading image failed", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onPrepareLoad(final Drawable placeHolderDrawable) {
                    Log.d("TAG", "Prepare Load");
                    Toast.makeText(MainActivity.this, "Downloading image", Toast.LENGTH_SHORT).show();
                }
            });
    
        }
    
    }
    

    Make sure:

    • you use this import import com.squareup.picasso.Target; instead of import java.lang.annotation.Target;
    • you give INTERNET permission and SET_WALLPAPER permission in your AndroidManifest.xml. <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.SET_WALLPAPER" />

    Lastly: wait for the image to download then check your device home screen and voila!!!