SoFunction
Updated on 2025-04-07

Android reading network image sample code analysis

On Android phones, we often use ImageView to display pictures. In this chapter, we obtain network pictures and display them in ImageView.

1. Design interface

1. Layout files

Open the res/layout/activity_main.xml file.

Enter the following code:

<?xml version="." encoding="utf-"?> 
<LinearLayout xmlns:andro 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 
<ImageView 
android: 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 
</LinearLayout> 

2. Program files

Open the "src//" file.

Then enter the following code:

package ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
public class MainActivity extends Activity { 
private ImageView imView; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
(savedInstanceState); 
setContentView(.activity_main); 
imView = (ImageView) findViewById(); 
String imageUrl = "/img/image/"; 
new NetworkPhoto().execute(imageUrl); 
} 
/* Four steps:
 * ()onPreExecute(), executes preprocessing, it runs on UI thread,
 * You can do some preparation for background tasks, such as drawing a progress bar control. 
 * ()doInBackground(Params...), the specific calculations executed by the background process are implemented here.
 * doInBackground(Params...) is the key to AsyncTask, and this method must be overloaded. 
 * In this method, you can use publishProgress(Progress...) to change the current progress value. 
 * ()onProgressUpdate(Progress...), runs on the UI thread.  if 
 * If you use publishProgress(Progress...) in doInBackground(Params...), you will
 * Trigger this method.  Here, the progress bar control can be responded specifically to the progress value. 
 * ()onPostExecute(Result), runs on UI thread, can process the results of background tasks, the results
 * is the return value of doInBackground(Params...).  This method must also be overloaded frequently, if the Result is
 * null indicates that the background task has not been completed (cancelled or an exception has occurred).  *
 */ 
//We only used () and () in this caseclass NetworkPhoto extends AsyncTask&lt;String, Integer, Bitmap&gt; { 
public NetworkPhoto() { 
} 
//doInBackground(Params...), the specific calculations executed by the background process are implemented here, which is the key to AsyncTask, and this method must be overloaded.@Override 
protected Bitmap doInBackground(String... urls) { 
URL url = null; 
Bitmap bitmap = null; 
HttpURLConnection conn=null; 
InputStream is=null; 
try { 
url = new URL(urls[]); 
} catch (MalformedURLException e) { 
(); 
} 
try { 
conn = (HttpURLConnection) (); 
(true); 
(); 
is = (); 
bitmap = (is); 
(); 
} catch (IOException e) { 
(); 
} finally { 
if(conn!=null){ 
(); 
conn=null; 
} 
if(is!=null) { 
try { 
(); 
} catch (IOException e) { 
(); 
} 
is=null; 
} 
} 
return bitmap; 
} 
//onPostExecute(Result), runs on UI thread, can process the results of background tasks, the results are//It is the return value of doInBackground(Params...).@Override 
protected void onPostExecute(Bitmap bitmap) { 
// Return the result bitmap is displayed in the ImageView control(bitmap); 
} 
} 
}

3. Configuration file

Open the "" file.

Then enter the following code:

<?xml version="." encoding="utf-"?> 
<manifest xmlns:andro 
package="" 
android:versionCode="" 
android:versionName="." > 
<uses-sdk 
android:minSdkVersion="" 
android:targetSdkVersion="" /> 
<uses-permission android:name="" /> 
<application 
android:allowBackup="true" 
android:icon="@drawable/ic_launcher" 
android:label="@string/app_name" 
android:theme="@style/AppTheme" > 
<activity 
android:name="" 
android:label="@string/app_name" > 
<intent-filter> 
<action android:name="" /> 
<category android:name="" /> 
</intent-filter> 
</activity> 
</application> 
</manifest> 

Note: You need to add permissions to the file:

 <uses-permission android:name="" />