Android Picture Download problem

Source: Internet
Author: User

============ Problem Description ============

Who is the great God to help me see the following code, why the incoming URL finally get the drawable is empty?
  Network images are downloaded to the local cache directory and saved to the Imagurl image file name. If a file with the same name is loaded locally from the cache directory Public static drawable loadimagefromurl (context context, string  imageurl)  {Drawable drawable = null;if  (imageurl == null) return  Null string imagepath =  ""; string filename =  "";//  gets the filename and suffix of the picture in the URL if  (imageurl != null &&  imageurl.length ()  != 0)  {filename = imageurl.substring (ImageUrl.lastIndexOf ("/") )  + 1);}   picture on the phone local storage path, note: filename is empty case imagepath = context.getcachedir ()  +  "/"  +  FileName; LOG.I (tag,  "imagepath = "  + imagepath); File file = new file (Context.getcachedir (),  filename);//  save File System.out.println ( "cachedir = "  + context.getcachedir ()); LOG.I (tag,  "file.tostring () ="  + file.tostring ());if  (!file.exists ()  && ! File.isdirectory ())  {try {//  can be used here by the filename to determine whether there is a local image fileoutputstream fos = new  fileoutputstream (file); Inputstream is = new url (IMAGEURL). OpenStream (); int data  = ();while  (data != -1)  {fos.write (data);d ata = (); Fos.close (); Is.close ();// drawable = drawable.createfromstream (// new url (IMAGEURL). OpenStream (),  file.tostring ()  ); ////  (InputStream)  new url (IMAGEURL). getContent ();d Rawable = drawable.createfrompath (file.tostring ()); LOG.I (tag,  "file.exists () no file exists, download online:"  + drawable.tostring ());}  catch  (ioexception e)  {log.e (tag, e.tostring ()  +  "image download and save when there is an exception! ");}}  else {drawable = drawable.createfrompath (File.tostring ()); LOG.I ("Test",  "file.tostring ():"  + file.tostring ()); LOG.I ("Test",  "file.exists () file exists, obtained locally:"  + drawable);} return drawable;}

Here is the output:

============ Solution 1============

Reference 4 Floor u014274707 's reply:
Quote: Referring to the reply of the 3 floor Svenwang:

Quote: Referring to the reply of the 2 floor u014274707:

Quote: References  1  floor  svenwang  reply:

Your The problem arises:
1. The first call to the function, because it does not exist locally, so go to the network download.
2. Because you access the network in the UI thread, it causes an exception and generates an empty file with a length of 0 locally.
3. The second call to the function, because the local file exists, so the Else branch is executed
4. Because the contents of the file are empty, Drawable.createfrompath returns a null

1. Do not in the UI line Download files in the process.
2. Delete residual files when the download file has an exception. The problem with
is that I didn't download it in the UI thread, and Logcat did not report exception, I was calling the method on the line constructor. As follows:
executor = new threadpoolexecutor (1, 50, 180,  Timeunit.seconds, queue);//  Use the thread pool to do the task of downloading pictures Executor.execute (new runnable ()  {@Overridepublic  void run ()  {drawable drawable = loadimagefromurl (CONTEXT,&NBSP;IMAGEURL); Imagecache.put (imageurl, new softreference<drawable> (drawable));
Message message = handler.obtainmessage (0, drawable); handler.sendmessage (message);}); 

This could be caused by the failure of the download to cause the contents of the local file to be incomplete. You can check the/data/data/com.roy.activity/cache/test01_upload_1.jpg.
This file.
What is the reason for the download failure? What can I do to make the picture display correctly?

Did you check the local/data/data/com.roy.activity/cache/test01_upload_1.jpg file for a problem?
Because the network causes download interruption or the failure is very common thing, the specific reason depends on the information in the exception.

Android Picture Download problem

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: 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.