Android Development's method of loading pictures _android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.