List of usage examples for android.graphics Bitmap recycle
public void recycle()
From source file:com.xmobileapp.rockplayer.LastFmAlbumArtImporter.java
/********************************* * // www. j a v a2s . c o m * creatAlbumArt * *********************************/ public Bitmap createAlbumArt(String artistName, String albumName, String albumArtURL) { String fileName = ((RockPlayer) context).FILEX_ALBUM_ART_PATH + validateFileName(artistName) + " - " + validateFileName(albumName) + FILEX_FILENAME_EXTENSION; validateFileName(fileName); File albumArtFile = new File(fileName); try { /* * Retreive Album */ albumArtFile.createNewFile(); FileOutputStream albumArtFileStream = new FileOutputStream(albumArtFile); /* * retreive URL */ BasicHttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 10000); HttpConnectionParams.setSoTimeout(params, 10000); DefaultHttpClient httpClient = new DefaultHttpClient(params); // DefaultedHttpParams params = new DefaultedHttpParams(httpClient.getParams(), // httpClient.getParams()); // httpClient.setParams(params); HttpGet httpGet = new HttpGet(albumArtURL); HttpResponse response; HttpEntity entity; InputStream albumArtURLStream = null; response = httpClient.execute(httpGet); entity = response.getEntity(); // BufferedReader in = // new BufferedReader(new InputStreamReader( // entity.getContent())); albumArtURLStream = entity.getContent(); // URL albumArt = new URL(albumArtURL); // InputStream albumArtURLStream = albumArt.openStream(); byte buf[] = new byte[1024]; int len; while ((len = albumArtURLStream.read(buf)) >= 0) albumArtFileStream.write(buf, 0, len); albumArtFileStream.close(); albumArtURLStream.close(); /* * Rescale/Crop the Bitmap */ BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; Bitmap albumArtBitmap = BitmapFactory.decodeFile(fileName, opts); if (opts.outHeight < 320 || opts.outWidth < 320) return null; int MAX_DIM = 460; int sampling = 1; if (Math.min(opts.outWidth, opts.outHeight) > MAX_DIM) { sampling = (int) Math.floor(Math.min(opts.outWidth, opts.outHeight) / MAX_DIM); } opts.inSampleSize = sampling; opts.inJustDecodeBounds = false; FileInputStream fileNameStream = new FileInputStream(fileName); albumArtBitmap = BitmapFactory.decodeStream(fileNameStream, null, opts); fileNameStream.close(); if (albumArtBitmap != null) { int dimension = Math.min(480, Math.min(albumArtBitmap.getHeight(), albumArtBitmap.getWidth())); Bitmap albumArtBitmapRescaled = Bitmap.createBitmap(albumArtBitmap, (int) Math.floor((albumArtBitmap.getWidth() - dimension) / 2), (int) Math.floor((albumArtBitmap.getHeight() - dimension) / 2), (int) dimension, (int) dimension); FileOutputStream rescaledAlbumArtFileStream; rescaledAlbumArtFileStream = new FileOutputStream(albumArtFile); albumArtBitmapRescaled.compress(Bitmap.CompressFormat.JPEG, 96, rescaledAlbumArtFileStream); rescaledAlbumArtFileStream.close(); if (albumArtBitmapRescaled != null) albumArtBitmapRescaled.recycle(); } else { albumArtFile.delete(); } if (albumArtBitmap != null) albumArtBitmap.recycle(); return albumArtBitmap; } catch (Exception e) { e.printStackTrace(); albumArtFile.delete(); return null; } catch (Error err) { err.printStackTrace(); return null; } }
From source file:com.android.launcher2.AsyncTaskCallback.java
void cleanup(boolean cancelled) { // Clean up any references to source/generated bitmaps if (sourceImages != null) { if (cancelled) { for (Bitmap b : sourceImages) { b.recycle(); }//w w w.ja va2s. com } sourceImages.clear(); } if (generatedImages != null) { if (cancelled) { for (Bitmap b : generatedImages) { b.recycle(); } } generatedImages.clear(); } }
From source file:mobisocial.musubi.util.UriImage.java
/** * Returns the bytes for this UriImage. If the uri for the image is remote, * then this code must not be run on the main thread. *///from ww w . j a v a 2s.co m public byte[] getResizedImageData(int widthLimit, int heightLimit, int byteLimit, boolean square) throws IOException { if (!mDecodedBounds) { decodeBoundsInfo(); mDecodedBounds = true; } InputStream input = null; try { int inDensity = 0; int targetDensity = 0; BitmapFactory.Options read_options = new BitmapFactory.Options(); read_options.inJustDecodeBounds = true; input = openInputStream(mUri); BitmapFactory.decodeStream(input, null, read_options); if (read_options.outWidth > widthLimit || read_options.outHeight > heightLimit) { //we need to scale if (read_options.outWidth / widthLimit > read_options.outHeight / heightLimit) { //width is the large edge if (read_options.outWidth * heightLimit > widthLimit * read_options.outHeight) { //incoming image is wider than target inDensity = read_options.outWidth; targetDensity = widthLimit; } else { //incoming image is taller than target inDensity = read_options.outHeight; targetDensity = heightLimit; } } else { //height is the long edge, swap the limits if (read_options.outWidth * widthLimit > heightLimit * read_options.outHeight) { //incoming image is wider than target inDensity = read_options.outWidth; targetDensity = heightLimit; } else { //incoming image is taller than target inDensity = read_options.outHeight; targetDensity = widthLimit; } } } else { //no scale if (read_options.outWidth > read_options.outHeight) { inDensity = targetDensity = read_options.outWidth; } else { inDensity = targetDensity = read_options.outHeight; } } if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "getResizedImageData: wlimit=" + widthLimit + ", hlimit=" + heightLimit + ", sizeLimit=" + byteLimit + ", mWidth=" + mWidth + ", mHeight=" + mHeight + ", initialRatio=" + targetDensity + "/" + inDensity); } ByteArrayOutputStream os = null; int attempts = 1; int lowMemoryReduce = 1; do { BitmapFactory.Options options = new BitmapFactory.Options(); options.inDensity = inDensity; options.inSampleSize = lowMemoryReduce; options.inScaled = lowMemoryReduce == 1; options.inTargetDensity = targetDensity; //no purgeable because we are only trying to resave this if (input != null) input.close(); input = openInputStream(mUri); int quality = IMAGE_COMPRESSION_QUALITY; try { Bitmap b = BitmapFactory.decodeStream(input, null, options); if (b == null) { return null; } if (options.outWidth > widthLimit + 1 || options.outHeight > heightLimit + 1) { // The decoder does not support the inSampleSize option. // Scale the bitmap using Bitmap library. int scaledWidth; int scaledHeight; scaledWidth = options.outWidth * targetDensity / inDensity; scaledHeight = options.outHeight * targetDensity / inDensity; if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "getResizedImageData: retry scaling using " + "Bitmap.createScaledBitmap: w=" + scaledWidth + ", h=" + scaledHeight); } if (square) { int w = b.getWidth(); int h = b.getHeight(); int dim = Math.min(w, h); b = Bitmap.createBitmap(b, (w - dim) / 2, (h - dim) / 2, dim, dim); scaledWidth = dim; scaledHeight = dim; } Bitmap b2 = Bitmap.createScaledBitmap(b, scaledWidth, scaledHeight, false); b.recycle(); b = b2; if (b == null) { return null; } } Matrix matrix = new Matrix(); if (mRotation != 0f) { matrix.preRotate(mRotation); } Bitmap old = b; b = Bitmap.createBitmap(old, 0, 0, old.getWidth(), old.getHeight(), matrix, true); // Compress the image into a JPG. Start with MessageUtils.IMAGE_COMPRESSION_QUALITY. // In case that the image byte size is still too large reduce the quality in // proportion to the desired byte size. Should the quality fall below // MINIMUM_IMAGE_COMPRESSION_QUALITY skip a compression attempt and we will enter // the next round with a smaller image to start with. os = new ByteArrayOutputStream(); b.compress(CompressFormat.JPEG, quality, os); int jpgFileSize = os.size(); if (jpgFileSize > byteLimit) { int reducedQuality = quality * byteLimit / jpgFileSize; //always try to squish it before computing the new size if (reducedQuality < MINIMUM_IMAGE_COMPRESSION_QUALITY) { reducedQuality = MINIMUM_IMAGE_COMPRESSION_QUALITY; } quality = reducedQuality; if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "getResizedImageData: compress(2) w/ quality=" + quality); } os = new ByteArrayOutputStream(); b.compress(CompressFormat.JPEG, quality, os); } b.recycle(); // done with the bitmap, release the memory } catch (java.lang.OutOfMemoryError e) { Log.w(TAG, "getResizedImageData - image too big (OutOfMemoryError), will try " + " with smaller scale factor, cur scale factor", e); lowMemoryReduce *= 2; // fall through and keep trying with a smaller scale factor. } if (true || Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "attempt=" + attempts + " size=" + (os == null ? 0 : os.size()) + " width=" + options.outWidth + " height=" + options.outHeight + " Ratio=" + targetDensity + "/" + inDensity + " quality=" + quality); } //move halfway to the target targetDensity = (os == null) ? (int) (targetDensity * .8) : (targetDensity * byteLimit / os.size() + targetDensity) / 2; attempts++; } while ((os == null || os.size() > byteLimit) && attempts < NUMBER_OF_RESIZE_ATTEMPTS); return os == null ? null : os.toByteArray(); } catch (Throwable t) { Log.e(TAG, t.getMessage(), t); return null; } finally { if (input != null) { try { input.close(); } catch (IOException e) { Log.e(TAG, e.getMessage(), e); } } } }
From source file:mobisocial.noteshere.util.UriImage.java
/** * Returns the bytes for this UriImage. If the uri for the image is remote, * then this code must not be run on the main thread. *///from w w w . ja va 2 s .c o m public byte[] getResizedImageData(int widthLimit, int heightLimit, int byteLimit, boolean square) throws IOException { if (!mDecodedBounds) { decodeBoundsInfo(); mDecodedBounds = true; } InputStream input = null; try { int inDensity = 0; int targetDensity = 0; BitmapFactory.Options read_options = new BitmapFactory.Options(); read_options.inJustDecodeBounds = true; input = openInputStream(mUri); BitmapFactory.decodeStream(input, null, read_options); if (read_options.outWidth > widthLimit || read_options.outHeight > heightLimit) { //we need to scale if (read_options.outWidth / widthLimit > read_options.outHeight / heightLimit) { //width is the large edge if (read_options.outWidth * heightLimit > widthLimit * read_options.outHeight) { //incoming image is wider than target inDensity = read_options.outWidth; targetDensity = widthLimit; } else { //incoming image is taller than target inDensity = read_options.outHeight; targetDensity = heightLimit; } } else { //height is the long edge, swap the limits if (read_options.outWidth * widthLimit > heightLimit * read_options.outHeight) { //incoming image is wider than target inDensity = read_options.outWidth; targetDensity = heightLimit; } else { //incoming image is taller than target inDensity = read_options.outHeight; targetDensity = widthLimit; } } } else { //no scale if (read_options.outWidth > read_options.outHeight) { inDensity = targetDensity = read_options.outWidth; } else { inDensity = targetDensity = read_options.outHeight; } } if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "getResizedImageData: wlimit=" + widthLimit + ", hlimit=" + heightLimit + ", sizeLimit=" + byteLimit + ", mWidth=" + mWidth + ", mHeight=" + mHeight + ", initialRatio=" + targetDensity + "/" + inDensity); } ByteArrayOutputStream os = null; int attempts = 1; int lowMemoryReduce = 1; do { BitmapFactory.Options options = new BitmapFactory.Options(); options.inDensity = inDensity; options.inSampleSize = lowMemoryReduce; options.inScaled = lowMemoryReduce == 1; options.inTargetDensity = targetDensity; //no purgeable because we are only trying to resave this if (input != null) input.close(); input = openInputStream(mUri); int quality = IMAGE_COMPRESSION_QUALITY; try { Bitmap b = BitmapFactory.decodeStream(input, null, options); if (b == null) { return null; } if (options.outWidth > widthLimit + 1 || options.outHeight > heightLimit + 1) { // The decoder does not support the inSampleSize option. // Scale the bitmap using Bitmap library. int scaledWidth; int scaledHeight; scaledWidth = options.outWidth * targetDensity / inDensity; scaledHeight = options.outHeight * targetDensity / inDensity; if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "getResizedImageData: retry scaling using " + "Bitmap.createScaledBitmap: w=" + scaledWidth + ", h=" + scaledHeight); } if (square) { int w = b.getWidth(); int h = b.getHeight(); int dim = Math.min(w, h); b = Bitmap.createBitmap(b, (w - dim) / 2, (h - dim) / 2, dim, dim); scaledWidth = dim; scaledHeight = dim; } Bitmap b2 = Bitmap.createScaledBitmap(b, scaledWidth, scaledHeight, false); b.recycle(); b = b2; if (b == null) { return null; } } Matrix matrix = new Matrix(); if (mRotation != 0f) { matrix.preRotate(mRotation); } Bitmap old = b; b = Bitmap.createBitmap(old, 0, 0, old.getWidth(), old.getHeight(), matrix, true); // Compress the image into a JPG. Start with MessageUtils.IMAGE_COMPRESSION_QUALITY. // In case that the image byte size is still too large reduce the quality in // proportion to the desired byte size. Should the quality fall below // MINIMUM_IMAGE_COMPRESSION_QUALITY skip a compression attempt and we will enter // the next round with a smaller image to start with. os = new ByteArrayOutputStream(); b.compress(CompressFormat.JPEG, quality, os); int jpgFileSize = os.size(); if (jpgFileSize > byteLimit) { int reducedQuality = quality * byteLimit / jpgFileSize; //always try to squish it before computing the new size if (reducedQuality < MINIMUM_IMAGE_COMPRESSION_QUALITY) { reducedQuality = MINIMUM_IMAGE_COMPRESSION_QUALITY; } quality = reducedQuality; if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "getResizedImageData: compress(2) w/ quality=" + quality); } os = new ByteArrayOutputStream(); b.compress(CompressFormat.JPEG, quality, os); } b.recycle(); // done with the bitmap, release the memory } catch (java.lang.OutOfMemoryError e) { Log.w(TAG, "getResizedImageData - image too big (OutOfMemoryError), will try " + " with smaller scale factor, cur scale factor", e); lowMemoryReduce *= 2; // fall through and keep trying with a smaller scale factor. } Log.v(TAG, "attempt=" + attempts + " size=" + (os == null ? 0 : os.size()) + " width=" + options.outWidth + " height=" + options.outHeight + " Ratio=" + targetDensity + "/" + inDensity + " quality=" + quality); //move halfway to the target targetDensity = (os == null) ? (int) (targetDensity * .8) : (targetDensity * byteLimit / os.size() + targetDensity) / 2; attempts++; } while ((os == null || os.size() > byteLimit) && attempts < NUMBER_OF_RESIZE_ATTEMPTS); return os == null ? null : os.toByteArray(); } catch (Throwable t) { Log.e(TAG, t.getMessage(), t); return null; } finally { if (input != null) { try { input.close(); } catch (IOException e) { Log.e(TAG, e.getMessage(), e); } } } }
From source file:org.planetmono.dcuploader.ActivityUploader.java
private Bitmap getBitmapThumbnail(Uri uri) { Bitmap b = null; try {/*from w ww .j a va 2s.co m*/ b = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); } catch (FileNotFoundException e1) { try { b = BitmapFactory.decodeStream(new FileInputStream(uri.getPath())); } catch (FileNotFoundException e2) { return Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_4444); } } int orientation = queryOrientation(uri); Bitmap resized = BitmapHelper.getResizedBitmap(b, 64, BitmapHelper.Axis.Vertical, orientation); b.recycle(); return resized; }
From source file:edu.mit.mobile.android.locast.data.MediaSync.java
private String generateThumbnail(Uri castMedia, String mimeType, String locMedia) throws ThumbnailException { final long locId = ContentUris.parseId(Uri.parse(locMedia)); Bitmap thumb; if (mimeType.startsWith("image/")) { thumb = Images.Thumbnails.getThumbnail(getContentResolver(), locId, Images.Thumbnails.MINI_KIND, null); } else if (mimeType.startsWith("video/")) { thumb = Video.Thumbnails.getThumbnail(getContentResolver(), locId, Video.Thumbnails.MINI_KIND, null); } else {/*from ww w . ja v a 2s.c om*/ throw new IllegalArgumentException( "cannot generate thumbnail for item with MIME type: '" + mimeType + "'"); } if (thumb == null) { throw new ThumbnailException("Android thumbnail generator returned null"); } try { final File outFile = new File(getCacheDir(), "thumb" + sha1Sum(locMedia) + ".jpg"); // final File outFile = File.createTempFile("thumb", ".jpg", // getCacheDir()); if (!outFile.exists()) { if (!outFile.createNewFile()) { throw new IOException("cannot create new file"); } if (DEBUG) { Log.d(TAG, "attempting to save thumb in " + outFile); } final FileOutputStream fos = new FileOutputStream(outFile); thumb.compress(CompressFormat.JPEG, 75, fos); thumb.recycle(); fos.close(); if (DEBUG) { Log.d(TAG, "generated thumbnail for " + locMedia + " and saved it in " + outFile.getAbsolutePath()); } } return Uri.fromFile(outFile).toString(); } catch (final IOException ioe) { final ThumbnailException te = new ThumbnailException(); te.initCause(ioe); throw te; } }
From source file:com.affectiva.affdexme.MainActivity.java
private void processScreenshot(Bitmap drawingViewBitmap, boolean alsoSaveRaw) { if (mostRecentFrame == null) { Toast.makeText(getApplicationContext(), "No frame detected, aborting screenshot", Toast.LENGTH_SHORT) .show();/*from w w w. j a v a 2 s.com*/ return; } if (!storagePermissionsAvailable) { checkForStoragePermissions(); return; } Bitmap faceBitmap = ImageHelper.getBitmapFromFrame(mostRecentFrame); if (faceBitmap == null) { Log.e(LOG_TAG, "Unable to generate bitmap for frame, aborting screenshot"); return; } metricViewLayout.setDrawingCacheEnabled(true); Bitmap metricsBitmap = Bitmap.createBitmap(metricViewLayout.getDrawingCache()); metricViewLayout.setDrawingCacheEnabled(false); Bitmap finalScreenshot = Bitmap.createBitmap(faceBitmap.getWidth(), faceBitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(finalScreenshot); Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG); canvas.drawBitmap(faceBitmap, 0, 0, paint); float scaleFactor = ((float) faceBitmap.getWidth()) / ((float) drawingViewBitmap.getWidth()); int scaledHeight = Math.round(drawingViewBitmap.getHeight() * scaleFactor); canvas.drawBitmap(drawingViewBitmap, null, new Rect(0, 0, faceBitmap.getWidth(), scaledHeight), paint); scaleFactor = ((float) faceBitmap.getWidth()) / ((float) metricsBitmap.getWidth()); scaledHeight = Math.round(metricsBitmap.getHeight() * scaleFactor); canvas.drawBitmap(metricsBitmap, null, new Rect(0, 0, faceBitmap.getWidth(), scaledHeight), paint); metricsBitmap.recycle(); drawingViewBitmap.recycle(); Date now = new Date(); String timestamp = DateFormat.format("yyyy-MM-dd_hh-mm-ss", now).toString(); File pictureFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "AffdexMe"); if (!pictureFolder.exists()) { if (!pictureFolder.mkdir()) { Log.e(LOG_TAG, "Unable to create directory: " + pictureFolder.getAbsolutePath()); return; } } String screenshotFileName = timestamp + ".png"; File screenshotFile = new File(pictureFolder, screenshotFileName); try { ImageHelper.saveBitmapToFileAsPng(finalScreenshot, screenshotFile); } catch (IOException e) { String msg = "Unable to save screenshot"; Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show(); Log.e(LOG_TAG, msg, e); return; } ImageHelper.addPngToGallery(getApplicationContext(), screenshotFile); if (alsoSaveRaw) { String rawScreenshotFileName = timestamp + "_raw.png"; File rawScreenshotFile = new File(pictureFolder, rawScreenshotFileName); try { ImageHelper.saveBitmapToFileAsPng(faceBitmap, rawScreenshotFile); } catch (IOException e) { String msg = "Unable to save screenshot"; Log.e(LOG_TAG, msg, e); } ImageHelper.addPngToGallery(getApplicationContext(), rawScreenshotFile); } faceBitmap.recycle(); finalScreenshot.recycle(); String fileSavedMessage = "Screenshot saved to: " + screenshotFile.getPath(); Toast.makeText(getApplicationContext(), fileSavedMessage, Toast.LENGTH_SHORT).show(); Log.d(LOG_TAG, fileSavedMessage); }
From source file:com.polyvi.xface.extension.camera.XCameraExt.java
private void photoSucess(Intent intent) { //URI????URI?URI?? //???try-catch??? Uri uri = intent.getData();/*from ww w . jav a 2 s. c o m*/ if (null == uri) { uri = mImageUri; } ContentResolver resolver = getContext().getContentResolver(); XPathResolver pathResolver = new XPathResolver(null == uri ? null : uri.toString(), "", getContext()); Bitmap bitmap = null; try { if (!mAllowEdit) { String path = pathResolver.resolve(); if (!XStringUtils.isEmptyString(path)) { bitmap = XUtils.decodeBitmap(path); } } else { //??????Android??? bitmap = intent.getExtras().getParcelable("data"); //?????URI if (bitmap == null) { bitmap = getCroppedBitmap(intent); } } } catch (OutOfMemoryError e) { mCallbackCtx.error("OutOfMemoryError when decode image."); return; } if (mDestType == DATA_URL) { int rotate = 0; String[] cols = { MediaStore.Images.Media.ORIENTATION }; Cursor cursor = resolver.query(uri, cols, null, null, null); if (null != cursor) { cursor.moveToPosition(0); rotate = cursor.getInt(0); cursor.close(); } if (0 != rotate) { Matrix matrix = new Matrix(); matrix.setRotate(rotate); bitmap = bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); } bitmap = scaleBitmap(bitmap); processPicture(bitmap); bitmap.recycle(); bitmap = null; System.gc(); } else if (mTargetHeight > 0 && mTargetWidth > 0) { try { Bitmap scaleBitmap = scaleBitmap(bitmap); String fileName = XConfiguration.getInstance().getWorkDirectory() + RESIZED_PIC_NAME; OutputStream os = new FileOutputStream(fileName); scaleBitmap.compress(Bitmap.CompressFormat.JPEG, mQuality, os); os.close(); bitmap.recycle(); bitmap = null; scaleBitmap.recycle(); scaleBitmap = null; mCallbackCtx.success("file://" + fileName + "?" + System.currentTimeMillis()); System.gc(); } catch (Exception e) { mCallbackCtx.error("Error retrieving image."); return; } } else { mCallbackCtx.success(XConstant.FILE_SCHEME + pathResolver.resolve()); } }
From source file:com.eleybourn.bookcatalogue.utils.Utils.java
/** * Shrinks the passed image file spec into the specificed dimensions, and returns the bitmap. If the view * is non-null, the image is also placed in the view. * /*from ww w. ja v a 2s . c o m*/ * @param destView * @param filename * @param maxWidth * @param maxHeight * @param exact * * @return */ private static Bitmap shrinkFileIntoImageView(ImageView destView, String filename, int maxWidth, int maxHeight, boolean exact) { Bitmap bm = null; // Read the file to get file size BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inJustDecodeBounds = true; BitmapFactory.decodeFile(filename, opt); // If no size info, or a single pixel, assume file bad and set the 'alert' icon if (opt.outHeight <= 0 || opt.outWidth <= 0 || (opt.outHeight == 1 && opt.outWidth == 1)) { if (destView != null) destView.setImageResource(android.R.drawable.ic_dialog_alert); return null; } // Next time we don't just want the bounds, we want the file opt.inJustDecodeBounds = false; // Work out how to scale the file to fit in required bbox float widthRatio = (float) maxWidth / opt.outWidth; float heightRatio = (float) maxHeight / opt.outHeight; // Work out scale so that it fit exactly float ratio = widthRatio < heightRatio ? widthRatio : heightRatio; // Note that inSampleSize seems to ALWAYS be forced to a power of 2, no matter what we // specify, so we just work with powers of 2. int idealSampleSize = (int) android.util.FloatMath.ceil(1 / ratio); // This is the sample size we want to use // Get the nearest *bigger* power of 2. int samplePow2 = (int) Math.pow(2, Math.ceil(Math.log(idealSampleSize) / Math.log(2))); try { if (exact) { // Create one bigger than needed and scale it; this is an attempt to improve quality. opt.inSampleSize = samplePow2 / 2; if (opt.inSampleSize < 1) opt.inSampleSize = 1; Bitmap tmpBm = BitmapFactory.decodeFile(filename, opt); if (tmpBm == null) { // We ran out of memory, most likely // TODO: Need a way to try loading images after GC(), or something. Otherwise, covers in cover browser wil stay blank. Logger.logError( new RuntimeException("Unexpectedly failed to decode bitmap; memory exhausted?")); return null; } android.graphics.Matrix matrix = new android.graphics.Matrix(); // Fixup ratio based on new sample size and scale it. ratio = ratio / (1.0f / opt.inSampleSize); matrix.postScale(ratio, ratio); bm = Bitmap.createBitmap(tmpBm, 0, 0, opt.outWidth, opt.outHeight, matrix, true); // Recycle if original was not returned if (bm != tmpBm) { tmpBm.recycle(); tmpBm = null; } } else { // Use a scale that will make image *no larger than* the desired size if (ratio < 1.0f) opt.inSampleSize = samplePow2; bm = BitmapFactory.decodeFile(filename, opt); } } catch (OutOfMemoryError e) { return null; } // Set ImageView and return bitmap if (destView != null) destView.setImageBitmap(bm); return bm; }