Search code examples
androidimageviewandroid-glide

Set image From URL as wallpaper - Glide Library -


I'm trying to create a wallpapers application using Glide library, in my app I have 4 categories and each category I have range of images displayed it by their URLs like this picture:

enter image description here

And when I click on a picture Appears in this format:

enter image description here

I have problem with Set as wallpaper I tried everything but doesn't work for me, this is my file.java responsible for fetching a picture and showing it.

public class Pop extends Activity {
int width,height;
String url;
LinearLayout llDownloadWallpaper,llsetwallpapers;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pop);
    llDownloadWallpaper = (LinearLayout)findViewById(R.id.llDownloadWallpaper);
    llsetwallpapers = (LinearLayout)findViewById(R.id.llSetWallpaper);
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    this.width = dm.widthPixels;
    this.height = dm.heightPixels;
    getWindow().setLayout((int) (((double) this.width) * 0.9d), (int) (((double) this.height) * 0.75d));
    getIntent().getSerializableExtra("WallpaperURL");
    this.url = (String)getIntent().getSerializableExtra("WallpaperURL");
    Glide.with(getApplicationContext()).load(this.url).into((ImageView)findViewById(R.id.imageSelectTo));
    llDownloadWallpaper.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // download image
        }
    });
    final ImageView userImageView = (ImageView) findViewById(R.id.imageSelectTo);
    llsetwallpapers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // set as wallpapers
           /* try {
                WallpaperManager.getInstance(getApplicationContext()).setBitmap(Glide.with(getApplicationContext()).load(url).asBitmap().into(width,height).get());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }*/
          }
        });
     }
  }

XML

?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linealL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#65000000">

<ImageView
    android:id="@+id/imageSelectTo"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" />
<!-- Set as wallpaper button -->
<LinearLayout
    android:id="@+id/llSetWallpaper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="10dp"
    android:layout_marginStart="10dp"
    android:background="@drawable/btn_rounded_corner"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:layout_marginLeft="10dp"
    android:layout_alignParentLeft="true">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="@string/SetAsWallpaper"
        android:textColor="@color/white"
        android:textSize="18sp" />
</LinearLayout>

<!-- Download wallpaper button -->

<LinearLayout
    android:id="@+id/llDownloadWallpaper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="10dp"
    android:layout_marginEnd="10dp"
    android:background="@drawable/btn_rounded_corner"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="@string/Download"
        android:textColor="@color/white"
        android:textSize="18sp" />
   </LinearLayout>
</RelativeLayout>

Solution

  • Make this change in your code.

    llsetwallpapers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          Glide.with(this)
        .load(path)
        .asBitmap()
        .into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super 
                      Bitmap> glideAnimation) {
    
    
     WallpaperManager.getInstance(getApplicationContext()).setBitmap(resource);
    
            }
        });
    
        });
     }