Android asynchronous image loading method 1 (3)

Source: Internet
Author: User

Utils. Java is as follows:

Package cn. loadImages; import java. io. inputStream; import java. io. outputStream; import android. graphics. bitmap; import android. graphics. bitmapFactory; public class Utils {public static void copyStream (InputStream is, OutputStream OS) {final int buffer_size = 1024; try {byte [] bytes = new byte [buffer_size]; for (;) {int count = is. read (bytes, 0, buffer_size); if (count =-1) {break;} OS. write (bytes, 0, count) ;}} Catch (Exception ex) {}} // obtain the image thumbnail public static Bitmap getBitmapThumbnail (String filePath, int minSideLength, int maxNumOfPixels) {BitmapFactory. options options = new BitmapFactory. options (); // true, the actual bitmap object will not be returned // no memory space is allocated to it, but some decoded boundary information such as the image size can be obtained. options. inJustDecodeBounds = true; // at this time, rawBitmap is nullBitmap rawBitmap = BitmapFactory. decodeFile (filePath, options); if (rawBitmap = null) {System. out. Println ("rawBitmap is null");} // inSampleSize indicates that the thumbnail size is a fraction of the size of the original image, if the value is 3 //, the width and height of the thumbnail are 1/3 of the original image, and the image size is 1/9 of the original size. // calculate sampleSizeint sampleSize = computeSampleSize (options, minSideLength, maxNumOfPixels); // to read an image, you must set options. inJustDecodeBounds sets falseoptions. inJustDecodeBounds = false; options. inSampleSize = sampleSize; // the original image size is 625x690 90.2kB // test the call computeSampleSize (options, 100,200*100); // get sampleSize = 8 // get width and height Bits 79 and 87 // 79*8 = 632 87*8 = 696 Bitmap thumbnailBitmap = BitmapFactory. decodeFile (filePath, options); return thumbnailBitmap;} // reference: // computeSampleSize (BitmapFactory. options options, int minSideLength, int maxNumOfPixels) {int initialSize = computeInitialSampleSize (options, min SideLength, maxNumOfPixels); int roundedSize; if (initialSize <= 8) {roundedSize = 1; while (roundedSize <initialSize) {roundedSize <= 1 ;}} else {roundedSize = (initialSize + 7)/8*8;} return roundedSize;} private static int computeInitialSampleSize (BitmapFactory. options options, int minSideLength, int maxNumOfPixels) {// width of the original image, double w = options. outWidth; // The height of the original image, double h = options. outHeight; int lowe RBound = (maxNumOfPixels =-1 )? 1: (int) Math. ceil (Math. sqrt (w * h/maxNumOfPixels); int upperBound = (minSideLength =-1 )? 128: (int) Math. min (Math. floor (w/minSideLength), Math. floor (h/minSideLength); if (upperBound <lowerBound) {// return the larger one when there is no overlapping zone. return lowerBound;} if (maxNumOfPixels =-1) & (minSideLength =-1) {return 1;} else if (minSideLength =-1) {return lowerBound;} else {return upperBound ;}}}

 

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.