An example of this article describes the Android development method for loading pictures. Share to everyone for your reference. The specific analysis is as follows:
Loading a picture on a network requires the ability to configure access to the network in manifest, as follows:
<uses-permission android:name= "Android.permission.INTERNET"/>
If you do not configure this permission, it will error: Unknown host exception.
Package com.example.loadimgfromweb;
Import Java.io.InputStream;
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.os.AsyncTask;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.widget.ImageView; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {sup
Er.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
ImageView v = (imageview) Findviewbyid (R.ID.IMAGEVIEW1);
String url = "Yun_qi_img/134970028_200x150.jpg";
New Downloadimage (v). Execute (URL); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; the adds items to the action
Bar if it is present.
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
public class Downloadimage extends Asynctask<string, Void, bitmap> {ImageView imageview; Public DownloadimaGE (ImageView imageview) {this.imageview = ImageView;
} @Override protected Bitmap doinbackground (String ... urls) {string url = urls[0];
Bitmap tmpbitmap = null;
try {InputStream is = new Java.net.URL (URL). OpenStream ();
Tmpbitmap = Bitmapfactory.decodestream (IS);
catch (Exception e) {e.printstacktrace ();
return tmpbitmap;
@Override protected void OnPostExecute (Bitmap result) {Imageview.setimagebitmap (result);
}
}
}
The above example uses the Asynctask to interact with the front-end user interface. Avoid loading time too long, affect interface interaction.
If you are loading a local picture, go directly to:
ImageView v = (imageview) Findviewbyid (r.id.imageview1);
V.setimageresource (R.drawable.apic);
You can save your pictures locally by using Asynctask to load the pictures:
@Override
protected Bitmap doinbackground (String ... urls) {
string url = urls[0];
Bitmap tmpbitmap = null;
try {
InputStream is = new Java.net.URL (URL). OpenStream ();
URL u = new url (URL);
HttpURLConnection conn = (httpurlconnection) u.openconnection ();
Bitmap Bitmap = Bitmapfactory.decodestream (is);
Determine if there is a memory card
if (Environment.getexternalstoragestate (). Equals (
environment.media_mounted)) {
// Save picture to local
File Sdcarddir = Environment.getexternalstoragedirectory ();
String fileName = "a.jpg";
String FilePath = Sdcarddir + "/" + FileName;
FileOutputStream fos = new FileOutputStream (filePath);
Bitmap.compress (Bitmap.CompressFormat.PNG, FOS);
Close Flow
is.close ();
Conn.disconnect ();
return bitmap;
} catch (Exception e) {
e.printstacktrace ();
}
return tmpbitmap;
}
I hope this article will help you with your Android program.