Android converts the network's Url resource to Drawable resource mode

  • 2021-11-24 03:00:43
  • OfStack

Overview

In today's development study, I encountered a requirement to add an bing Daily 1 chart to the flash page of App. It's all simple, but when I got the Url of the picture, I had a very awkward problem. This is how to convert Url to Drawabl and add it to ImageView.

The Url for getting pictures here will not be mentioned. Let's look at how to convert Url to Drawable.

Url to Drawable

Let's look at the source code first:


private Drawable loadImageFromNetwork(String imageUrl) {
 Drawable drawable = null;
 try {
  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;
}

In this part, we convert the network Url resources into Drawable resources.

Then we analyze wave 1 according to our source code:

Prime Minister, we created an Drawable

Then we generate an Drawable resource from our Url resource.

In this way, our Url network resources are converted into Drawable.

Supplementary knowledge: Three methods of obtaining network pictures in Android from URL to Drawable

Getting network pictures in android is a time-consuming operation. If you get them directly, the application program may have no response (ANR: Application ES50Responding) dialog box. In this case, the general method is to use threads to implement time-consuming operations. Here are three ways to get url pictures:

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 { 
  //  You can judge whether there is this picture locally by the file name here  
  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 url picture:


mImageView = (ImageView)this.findViewById(R.id.imageThreadConcept) ; 
new Thread(new Runnable(){ 
 Drawable drawable = loadImageFromNetwork(IMAGE_URL); 
 @Override 
 public void run() { 
   
  // post()  The key is to get to UI The main thread updates the picture      
  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); 
  } 
} 

Related articles: