Get thumbnails based on the specified image path and size. This method has two benefits:
1. Using a smaller memory space, the bitmap obtained for the first time is actually null, just to read the width and height. The bitmap read for the second time is the image compressed according to the proportion, and the bitmap read for the third time is the desired thumbnail.
2. The thumbnail image is not stretched for the original image. Here we use the new tool ThumbnailUtils, version 2.2, and the images generated using this tool will not be stretched.
/** * @param imagePath * The path to the image * @param width * Specify the width of the output image * @param height * Specify the height of the output image * @return Generated thumbnail */ public static Bitmap getImageThumbnail(String imagePath, int width, int height) { Bitmap bitmap = null; options = new (); = true; // Get the width and height of this picture, note that the bitmap here is null bitmap = (imagePath, options); = false; // Set to false // Calculate the scaling ratio int h = ; int w = ; int beWidth = w / width; int beHeight = h / height; int be = 1; if (beWidth < beHeight) { be = beWidth; } else { be = beHeight; } if (be <= 0) { be = 1; } = be; //Read the picture again and read the zoomed bitmap. Note that this time you should set it to false bitmap = (imagePath, options); // Use ThumbnailUtils to create thumbnails, here you need to specify which Bitmap object to zoom in bitmap = (bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); return bitmap; } /** * Get thumbnails of video. First use ThumbnailUtils to create a thumbnail of a video, and then use ThumbnailUtils to generate thumbnails of a specified size. * If the width and height of the desired thumbnail are smaller than MICRO_KIND, then the type should use MICRO_KIND as the value of kind, which will save memory. * * @param videoPath * Video path * @param width * Specify the width of the output video thumbnail * @param height * Specify the height of the output video thumbnail * @param kind * Refer to the constants MINI_KIND and MICRO_KIND in the class. * Where, MINI_KIND: 512 x 384, MICRO_KIND: 96 x 96 * @return Video thumbnail of specified size */ public static Bitmap getVideoThumbnail(String videoPath, int width, int height, int kind) { Bitmap bitmap = null; // Get a thumbnail of the video bitmap = (videoPath, kind); ("w" + ()); ("h" + ()); bitmap = (bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); return bitmap; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.