'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:
And when I click on a picture Appears in this format:
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 1:[1]
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);
}
});
});
}
Solution 2:[2]
Iam not gone give you the exact answer, but i will give you some idea
First on click get the URL of the clicked image, Then Set that Image as a background image Like this.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
WallpaperManager wpm = WallpaperManager.getInstance(context);
InputStream ins = null;
try {
ins = new URL("http://cdn.wonderfulengineering.com/wp-content/uploads/2014/07/inspiring-creative-design-mobile-phone-wallpapers.jpg").openStream();
wpm.setStream(ins);
} catch (IOException e) {
e.printStackTrace();
}
Also don't forget to add the permission to manifest file
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
Solution 3:[3]
In my case its working,
wallpaperManager=WallpaperManager.getInstance(getApplicationContext());
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
// Button Onclick
setWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputStream ins = null;
try {
ins = new URL(url).openStream();
wallpaperManager.setStream(ins);
} catch (IOException e) {
e.printStackTrace();
}
FancyToast.makeText(getApplicationContext(), "Set Successfully",
FancyToast.LENGTH_SHORT, FancyToast.SUCCESS, false).show();
}
});
Please remember to add the setWallpaper and internet permission in AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
Solution 4:[4]
with Glid4+ I did like this:
private void setAsWallpaper() {
Glide.with(requireContext())
.asBitmap()
.load(listItem.get(position).getWallpaperImage())
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
try {
WallpaperManager.getInstance(requireContext()).setBitmap(resource);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dishonered |
Solution 2 | Sathya Baman |
Solution 3 | Nisha Jain |
Solution 4 | Sana Ebadi |