SoFunction
Updated on 2025-03-09

Android HttpURLConnection download network picture setting system wallpaper

need:

The wallpaper is a URL link, and get can request it, so use get to request the picture, convert the picture into a bitmap, and then set the wallpaper.

Code:

Here I encapsulate the tool class

package ;
 
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
 
/**
  * Set wallpaper
  * 1. Download the network image and use HttpURLConnection
  * 2. Set wallpaper
  * Created by zst on 2018/10/15.
  */
 
public class HttpURLConnectionUtil {
 
  /**
    * Set system wallpaper
    * 1. Set the network picture system wallpaper
    * 2. Because Google does not maintain other frameworks, use HttpURLConnection to download and configure
    *
    * @param activity
    * @param imgUrl
    */
  public static void setWallpaper(final Activity activity, final String imgUrl) {
    //("Wallpaper", "Link:" + imgUrl); 
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          URL httpUrl = new URL(imgUrl);//Get the incoming URL address and capture the exception generated by the parsing process          //Use is Http access, so use HttpURLConnection. Similarly, if you are using https, use HttpsURLConnection.          try {
            HttpURLConnection conn = (HttpURLConnection) ();// Open an HttpURLConnection object through httpUrl            (5000);//Set the display supermarket time to 5 seconds            ("GET");//Set access method            (true);//Set to get input stream 
            InputStream in = ();//Get input stream 
            //Create a file object to write to the ID card            FileOutputStream out = null;
            File download = null;
            String filename = (());//Get system time            //Determine whether the file exists Environment.MEDIA_MOUNTEDID card is mounted If so, create a file object            if (().equals(Environment.MEDIA_MOUNTED)) {
              File parent = ();//Get ID card directory              download = new File(parent, filename);//Create a file in the directory of the parent class with the current downloaded system time as the file name 
              out = new FileOutputStream(download);
            }
 
            byte[] b = new byte[2 * 1024];
            int len;
            if (out != null) {//If the id card exists, write it              while ((len = (b)) != -1) {
                (b, 0, len);
              }
            }
 
            //Read the contents in the file            final Bitmap bitmap = (());
            (new Runnable() {
              @Override
              public void run() {
                //Set the picture as wallpaper                //Bitmap bitmap = ((),.bg_user_top);//Set the picture in the project res                WallpaperManager manager = (activity);
                try {
                  (bitmap);
                  (activity, "The wallpaper is set up successfully, please view it on the desktop");
                } catch (IOException e) {
                  (activity, "Wallpaper setting failed");
                  ();
                }
              }
            });
 
          } catch (IOException e) {
            ();
          }
 
        } catch (MalformedURLException e) {
          ();
        }
 
      }
    }).start();
  }
 
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.