android--three ways to get pictures from the web

Source: Internet
Author: User

Getting a network picture in Android is a time-consuming operation if you get direct access to situations where the application's unresponsive (anr:application not responding) dialog box may appear. In this case, the general approach is to use the thread to implement the time-consuming operation. There are three ways to get a URL picture in the following columns:
1. Direct access: (easy: ANR, not recommended)
Mimageview = (ImageView) This.findviewbyid (r.id.imagethreadconcept);
drawable drawable = loadimagefromnetwork (Image_url);
Mimageview.setimagedrawable (drawable);

Common methods:
Private drawable Loadimagefromnetwork (String imageUrl)
{
Drawable drawable = null;
try {
This can be determined by the file name here, whether the picture is locally
drawable = Drawable.createfromstream (
New URL (IMAGEURL). OpenStream (), "image.jpg");
} catch (IOException e) {
LOG.D ("Test", E.getmessage ());
}
if (drawable = = null) {
LOG.D ("test", "null drawable");
} else {
LOG.D ("Test", "NOT null drawable");
}

return drawable;
}

2. Background thread gets the URL picture:

Mimageview = (ImageView) This.findviewbyid (r.id.imagethreadconcept);
New Thread (New Runnable () {
drawable drawable = loadimagefromnetwork (Image_url);
@Override
public void Run () {

Post () is especially important to update the image to the main UI thread.
Mimageview.post (New Runnable () {
@Override
public void Run () {
TODO auto-generated Method Stub
Mimageview.setimagedrawable (drawable);
}}) ;
}

}). Start ();

3.AsyncTask get URL picture

Mimageview = (ImageView) This.findviewbyid (r.id.imagethreadconcept);
New Downloadimagetask (). Execute (image_url);
Private class Downloadimagetask extends Asynctask<string, Void, drawable>
{

Protected drawable doinbackground (String ... urls) {
Return Loadimagefromnetwork (Urls[0]);
}

protected void OnPostExecute (drawable result) {
Mimageview.setimagedrawable (result);
}
}

Premise:
Download the Imageloader jar package and store it in the Libs folder
Address: Https://github.com/nostra13/Android-Universal-Image-Loader/tree/master/downloads

1. Create a new application under the SRC folder

1.1 The OnCreate () method is lowered with Initimageloader

  initImageLoader(getApplicationContext());

1.2 Define the Initimageloader method, mainly to complete the configuration

public static void Initimageloader (context context) {

 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) .threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .discCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO) .writeDebugLogs() // Remove for release app .build();

Imageloader.getinstance (). init (config);

}

2. Rewrite the name of application under the Manifest.xml file (which is the class that inherits the application just now), and register the network and other storage (??) before the beginning (application). ) method

(1 and 2 should actually be reversed, because if the changes in Manifest.xml are not saved, then 1 files will continue to be error-free)

 <application android:name=".XApplication" ...> </application> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

3, in the activity of the application
3.1 Instantiation of Imageloader

protected ImageLoader imageLoader =    ImageLoader.getInstance();

3.2 Configuring option

 options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.dog)//加载图片时显示的默认图片 .showImageForEmptyUri(R.drawable.girl)//访问地址无内容时显示的图片 .showImageOnFail(R.drawable.luobo)//加载失败时显示的图片 .cacheInMemory(true)//是否存储到内存中?? .cacheOnDisc(true)//是否加载到硬盘?SD卡?? .considerExifParams(true) .displayer(new RoundedBitmapDisplayer(20)) .build();

3.3 Call the DisplayImage () function to load the picture (it needs to be written in the place where the image is loaded specifically)

   imageLoader.displayImage(getItem(position).image, peopleImage, options);

(using a simpler function: The first argument is the URL of the call (String Class), the second is where you need to place the picture, not the R object, but the object in the Java file, such as Holder.image, and the third is the set parameter)

android--three ways to get pictures from the web

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.