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
<?xml version="1.0" encoding="utf-8"?> < 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=""> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Loading pictures"/> <ImageView android: android:layout_width="match_parent" android:layout_height="500dp" /> </LinearLayout> </>
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.