"Android" How to download GIF images to local

Source: Internet
Author: User

As you can see, although it is a JPG file, it is essentially a motion diagram.

But found in our picture module, the local storage of images only one frame, the problem is where?

Byte[] Data obtained by HTTP is no problem.

Breakpoint tracking, found that the problem occurred in the last sentence when compressing the size of the picture.

 Public StaticBitmap Getscaledbitmap (byte[] Data,intWidthintheight) {bitmapfactory.options Options=Newbitmapfactory.options (); Options.injustdecodebounds=true; Bitmapfactory.decodebytearray (data,0, data.length, Options); floatSrcwidth =Options.outwidth; floatSrcheight =Options.outheight; intInsamplesize = 1; if(Srcheight > Height | | srcwidth >width) {            if(Srcwidth >srcheight) Insamplesize= Math.Round (Srcheight/height); Elseinsamplesize= Math.Round (Srcwidth/width); } Options=Newbitmapfactory.options (); Options.insamplesize=insamplesize; returnBitmapfactory.decodebytearray (data, 0, data.length, Options); }

The final solution is to write to the local HTTP byte[] data without bitmap, and then compress the image size when it is removed.

 /*** Write Bytes * *@paramURL *@parambytes *@return     */     Public BooleanSave (String URL,byte[] bytes) {        if(bytes = =NULL|| Bytes.length = = 0)            return false; URL=trans2local (URL); File File=NewFile (URL); if(File.exists ())return true;        Zio.createnewfile (file); FileOutputStream Fos=NULL; Try{fos=Newfileoutputstream (file);            Fos.write (bytes); return true; } Catch(Exception e) {e.printstacktrace (); LOG.E ("Storage Error", E.getmessage ()); } finally {            Try{fos.close (); } Catch(Exception e) {e.printstacktrace (); }        }        return false; }

The extra benefit of this approach is to ignore the bizarre picture format quality issues.

For example, when we save the picture with the bitmap to take the judging picture type, but also to specify the compression accuracy (if 100, the picture size is much larger than the original, it is Strange)

    Bitmap.compressformat format = Url.tolowercase (). IndexOf ("PNG") > 0? Bitmap.CompressFormat.PNG:Bitmap.CompressFormat.JPEG;     , FOS);

"Android" How to download GIF images to local

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.