Android HttpURLConnection Download Network Picture Setting System Wallpaper
- 2021-10-16 02:47:09
- OfStack
Requirements:
Wallpaper is url link, get can be requested, so use get request to the picture, the picture into bitmap, and then set the wallpaper.
Code:
Here I encapsulate the tool class
package xxxxx.utils;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Set up wallpaper
* 1 Download network pictures and use HttpURLConnection
* 2 Set wallpaper
* Created by zst on 2018/10/15.
*/
public class HttpURLConnectionUtil {
/**
* Set up system wallpaper
* 1 Set the network picture to the system wallpaper
* 2 Because Google does not maintain other frameworks, it uses HttpURLConnection To download and configure
*
* @param activity
* @param imgUrl
*/
public static void setWallpaper(final Activity activity, final String imgUrl) {
//Log.e(" Wallpaper ", " Links: " + imgUrl);
new Thread(new Runnable() {
@Override
public void run() {
try {
URL httpUrl = new URL(imgUrl);// Gets the incoming url Address And catch exceptions generated by the parsing process
// Use is Http Visit So use HttpURLConnection Similarly, if you use https Then use HttpsURLConnection
try {
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();// Pass httpUrl Open 1 A HttpURLConnection Object
conn.setReadTimeout(5000);// Set the display time of supermarket to 5 Seconds
conn.setRequestMethod("GET");// Setting access methods
conn.setDoInput(true);// Set to get the input stream
InputStream in = conn.getInputStream();// Get the input stream
// Create 1 Write ID File object of card
FileOutputStream out = null;
File download = null;
String filename = String.valueOf(System.currentTimeMillis());// Get system time
// Determine whether the file exists Environment.MEDIA_MOUNTEDID Is the card mounted If yes, create a file object
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File parent = Environment.getExternalStorageDirectory();// Get ID Card directory
download = new File(parent, filename);// Create in the directory of the parent class 1 Files with the currently downloaded system time as the file name
out = new FileOutputStream(download);
}
byte[] b = new byte[2 * 1024];
int len;
if (out != null) {//id If the card exists Write to the
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
}
// Read the contents of the file
final Bitmap bitmap = BitmapFactory.decodeFile(download.getAbsolutePath());
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// Set the picture as wallpaper
//Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.bg_user_top);// Setting items res Pictures in
WallpaperManager manager = WallpaperManager.getInstance(activity);
try {
manager.setBitmap(bitmap);
UiUtil.showToastLong(activity, " The wallpaper was set successfully, please check it on the desktop ");
} catch (IOException e) {
UiUtil.showToast(activity, " Wallpaper set to failed ");
e.printStackTrace();
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}).start();
}
}