Set image as wallpaper from url

假如想象 提交于 2019-11-29 12:50:31

Rather than trying to download the image yourself and then having to process it. Instead use an image loading library like Picasso. With Picasso all you need to put into your click listener is:

Bitmap result=Picasso.with(context)
          .load(imageURL)
          .get();

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

Nice and easy without having to deal with threading.

You need Picasso library to get bitmap from url
To Add Latest Library https://github.com/square/picasso

String url ="https://..."
Target target = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        Log.e(TAG, "OnBitmapLoaded");
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(BroadcastService.this);
        try {
            wallpaperManager.setBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "IOException->" + e.getMessage());
        }
    }

    @Override
    public void onBitmapFailed(Exception e, Drawable errorDrawable) {
        Log.e(TAG, "" + e.getMessage());
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        Log.e(TAG, "OnPrepareLoad");
    }
};
Picasso.get().load(url).into(target);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!