SoFunction
Updated on 2025-04-10

How to get network pictures using threads

This article shares the specific code for Android to use threads to obtain network pictures for your reference. The specific content is as follows

   

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro
  package="">
  <uses-permission android:name=""></uses-permission>
  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:theme="@style/">
      <intent-filter>
        <action android:name="" />
 
        <category android:name="" />
      </intent-filter>
    </activity>
  </application>
 
</manifest>

activity_main.xml   

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt; xmlns:andro
  xmlns:app="/apk/res-auto"
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:context=""&gt;
 
  &lt;LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"&gt;
    &lt;Button
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Loading pictures"/&gt;
    &lt;ImageView
      android:
      android:layout_width="match_parent"
      android:layout_height="500dp" /&gt;
  &lt;/LinearLayout&gt;
 
&lt;/&gt;

   

package ;
 
import ;
import ;
import ;
import ;
import ;
import .;
import ;
import ;
import ;
 
import ;
import ;
import ;
import ;
import ;
 
public class MainActivity extends AppCompatActivity {
  private Button button;
  private ImageView imageView;
  private String imagPath = "/2007-11-09/200711912453162_2.jpg";
  private final int IS_FINISH = 1;
  private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      Bitmap bitmap = (Bitmap);
      (bitmap);
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    button = (Button)findViewById(.button1);
    imageView = (ImageView)findViewById(.image1);
    (new () {
      @Override
      public void onClick(View v) {
        new Thread(new MyClass()).start();
      }
    });
  }
 
  public class MyClass implements Runnable{
 
    @Override
    public void run() {
      Bitmap bitmap = null;
      try {
        URL url = new URL(imagPath);
        HttpURLConnection httpURLConnection = (HttpURLConnection)();
        (true);
        ();
        InputStream inputStream = ();
        bitmap = (inputStream);
      } catch (MalformedURLException e) {
        ();
      } catch (IOException e) {
        ();
      }
      Message message = ();
       = bitmap;
       = IS_FINISH;
      (message);
    }
  }
}

The above is all about this article, I hope it will be helpful for everyone to learn Android software programming.