List of usage examples for android.graphics BitmapFactory decodeFileDescriptor
public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts)
From source file:com.xxxifan.devbox.library.rxfile.RxFile.java
private static Observable<Bitmap> getThumbnailFromUriWithSizeAndKind(final Context context, final Uri data, final int requiredWidth, final int requiredHeight, final int kind) { return Observable.fromCallable(new Func0<Bitmap>() { @Override//from w w w . j av a 2s .c o m public Bitmap call() { Bitmap bitmap = null; ParcelFileDescriptor parcelFileDescriptor; final BitmapFactory.Options options = new BitmapFactory.Options(); if (requiredWidth > 0 && requiredHeight > 0) { options.inJustDecodeBounds = true; options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight); options.inJustDecodeBounds = false; } if (!isMediaUri(data)) { Log.e(TAG, "Not a media uri"); if (isGoogleDriveDocument(data)) { Log.e(TAG, "Google Drive Uri"); DocumentFile file = DocumentFile.fromSingleUri(context, data); if (file.getType().startsWith(Constants.IMAGE_TYPE) || file.getType().startsWith(Constants.VIDEO_TYPE)) { Log.e(TAG, "Google Drive Uri"); try { parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data, Constants.READ_MODE); FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); parcelFileDescriptor.close(); return bitmap; } catch (IOException e) { e.printStackTrace(); } } } else if (data.getScheme().equals(Constants.FILE)) { Log.e(TAG, "Dropbox or other content provider"); try { parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data, Constants.READ_MODE); FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); parcelFileDescriptor.close(); return bitmap; } catch (IOException e) { e.printStackTrace(); } } else { try { parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data, Constants.READ_MODE); FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); parcelFileDescriptor.close(); return bitmap; } catch (IOException e) { e.printStackTrace(); } } } else { Log.e(TAG, "Uri for thumbnail: " + data.toString()); Log.e(TAG, "Uri for thumbnail: " + data); String[] parts = data.getLastPathSegment().split(":"); String fileId = parts[1]; Cursor cursor = null; try { cursor = context.getContentResolver().query(data, null, null, null, null); if (cursor != null) { Log.e(TAG, "Cursor size: " + cursor.getCount()); if (cursor.moveToFirst()) { if (data.toString().contains(Constants.VIDEO)) { bitmap = MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(), Long.parseLong(fileId), kind, options); } else if (data.toString().contains(Constants.IMAGE)) { Log.e(TAG, "Image Uri"); bitmap = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(), Long.parseLong(fileId), kind, options); } Log.e(TAG, bitmap == null ? "null" : "not null"); } } return bitmap; } catch (Exception e) { Log.e(TAG, "Exception while getting thumbnail:" + e.getMessage()); } finally { if (cursor != null) cursor.close(); } } return bitmap; } }); }
From source file:com.mvc.imagepicker.ImagePicker.java
private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize) { Bitmap actuallyUsableBitmap = null;//from w ww. ja v a 2s.c om AssetFileDescriptor fileDescriptor = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = sampleSize; try { fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r"); actuallyUsableBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options); if (actuallyUsableBitmap != null) { Log.i(TAG, "Trying sample size " + options.inSampleSize + "\t\t" + "Bitmap width: " + actuallyUsableBitmap.getWidth() + "\theight: " + actuallyUsableBitmap.getHeight()); } fileDescriptor.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return actuallyUsableBitmap; }
From source file:com.ultramegasoft.flavordex2.util.PhotoUtils.java
/** * Load a Bitmap from an image file.//from www. j av a2 s .co m * * @param context The Context * @param uri The Uri to the image file * @param reqWidth The requested width of the decoded Bitmap * @param reqHeight The requested height of the decoded Bitmap * @return A Bitmap */ @Nullable public static Bitmap loadBitmap(@NonNull Context context, @NonNull Uri uri, int reqWidth, int reqHeight) { final ContentResolver cr = context.getContentResolver(); try { final ParcelFileDescriptor parcelFileDescriptor = cr.openFileDescriptor(uri, "r"); if (parcelFileDescriptor == null) { Log.w(TAG, "Unable to open " + uri.toString()); return null; } try { final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); final Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeFileDescriptor(fileDescriptor, null, opts); opts.inSampleSize = calculateInSampleSize(opts, reqWidth, reqHeight); opts.inJustDecodeBounds = false; final Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, opts); if ("image/jpeg".equals(opts.outMimeType)) { return rotatePhoto(context, uri, bitmap); } return bitmap; } catch (OutOfMemoryError e) { Log.e(TAG, "Out of memory", e); } finally { parcelFileDescriptor.close(); } } catch (FileNotFoundException e) { Log.w(TAG, "File not found: " + uri.toString()); } catch (SecurityException e) { Log.w(TAG, "Permission denied for Uri: " + uri.toString()); } catch (IOException e) { Log.e(TAG, "Failed to load bitmap", e); } return null; }
From source file:de.s2hmobile.bitmaps.ImageCache.java
/** * Decode and sample down a bitmap from a file input stream to the requested * width and height./*from www . java 2 s. c o m*/ * * @param fd * The file descriptor to read from * @param reqWidth * The requested width of the resulting bitmap * @param reqHeight * The requested height of the resulting bitmap * @param cache * The ImageCache used to find candidate bitmaps for use with * inBitmap * @return A bitmap sampled down from the original with the same aspect * ratio and dimensions that are equal to or greater than the * requested width and height */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) private Bitmap decodeSampledBitmapFromDescriptor(final FileDescriptor fd) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 1; options.inJustDecodeBounds = false; // If we're running on Honeycomb or newer, try to use inBitmap if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { options.inMutable = true; // Try and find a bitmap to use for inBitmap final Bitmap inBitmap = getBitmapFromReusableSet(options); if (inBitmap != null) { options.inBitmap = inBitmap; } } return BitmapFactory.decodeFileDescriptor(fd, null, options); }
From source file:java_lang_programming.com.android_media_demo.article94.java.ImageDecoderActivity.java
/** * Bitmap??// w w w . j a v a2s.c o m * * @param context * @param uri ?Uri * @return Bitmap */ private @Nullable Bitmap getBitmap(@NonNull Context context, @NonNull Uri uri) { final ParcelFileDescriptor parcelFileDescriptor; try { parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r"); } catch (FileNotFoundException e) { e.getStackTrace(); return null; } final FileDescriptor fileDescriptor; if (parcelFileDescriptor != null) { fileDescriptor = parcelFileDescriptor.getFileDescriptor(); } else { // ParcelFileDescriptor was null for given Uri: [" + mInputUri + "]" return null; } final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); if (options.outWidth == -1 || options.outHeight == -1) { // "Bounds for bitmap could not be retrieved from the Uri: [" + mInputUri + "]" return null; } int orientation = getOrientation(uri); int rotation = getRotation(orientation); Matrix transformMatrix = new Matrix(); transformMatrix.setRotate(rotation); // ??? WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; int reqWidth = Math.min(width, options.outWidth); int reqHeight = Math.min(height, options.outHeight); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; Bitmap decodeSampledBitmap = null; boolean decodeAttemptSuccess = false; while (!decodeAttemptSuccess) { try { decodeSampledBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); decodeAttemptSuccess = true; } catch (OutOfMemoryError error) { Log.e("", "doInBackground: BitmapFactory.decodeFileDescriptor: ", error); options.inSampleSize *= 2; } } // ?? decodeSampledBitmap = transformBitmap(decodeSampledBitmap, transformMatrix); return decodeSampledBitmap; }
From source file:android.com.example.contactslist.util.ImageLoader.java
/** * Decode and sample down a bitmap from a file input stream to the requested width and height. * * @param fileDescriptor The file descriptor to read from * @param reqWidth The requested width of the resulting bitmap * @param reqHeight The requested height of the resulting bitmap * @return A bitmap sampled down from the original with the same aspect ratio and dimensions * that are equal to or greater than the requested width and height *///from w ww. ja va2 s .c om public static Bitmap decodeSampledBitmapFromDescriptor(FileDescriptor fileDescriptor, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); }
From source file:com.app.chasebank.contactslist.util.ImageLoader.java
/** * Decode and sample down a bitmap from a file input stream to the requested width and height. * * @param fileDescriptor The file descriptor to read from * @param reqWidth The requested width of the resulting bitmap * @param reqHeight The requested height of the resulting bitmap * @return A bitmap sampled down from the original with the same aspect ratio and dimensions * that are equal to or greater than the requested width and height *///from w ww. j a v a 2 s. co m public static Bitmap decodeSampledBitmapFromDescriptor(FileDescriptor fileDescriptor, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); }
From source file:com.gh4a.utils.HttpImageGetter.java
private static Bitmap getBitmap(final File image, int width, int height) { final BitmapFactory.Options options = new BitmapFactory.Options(); RandomAccessFile file = null; try {/*from www . j a va 2 s . com*/ file = new RandomAccessFile(image.getAbsolutePath(), "r"); FileDescriptor fd = file.getFD(); options.inJustDecodeBounds = true; BitmapFactory.decodeFileDescriptor(fd, null, options); int scale = 1; while (options.outWidth >= width || options.outHeight >= height) { options.outWidth /= 2; options.outHeight /= 2; scale *= 2; } options.inJustDecodeBounds = false; options.inDither = false; options.inSampleSize = scale; return BitmapFactory.decodeFileDescriptor(fd, null, options); } catch (IOException e) { return null; } finally { if (file != null) { try { file.close(); } catch (IOException e) { // ignored } } } }
From source file:cut.ac.cy.my_tour_guide.gallery.ImageDownloader.java
public static Bitmap decodeSampledBitmapFromDescriptor(FileDescriptor fileDescriptor, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;// w w w .j a va 2 s . c o m BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); System.out.println(options.inSampleSize + "\n"); System.out.println(reqWidth + "\n"); System.out.println(reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); }
From source file:com.serenegiant.testmediastore.MediaStorePhotoAdapter.java
private static final Bitmap getImage(ContentResolver cr, long id, int requestWidth, int requestHeight) throws FileNotFoundException, IOException { Bitmap result = null;//from ww w. j a va 2 s .c o m final ParcelFileDescriptor pfd = cr.openFileDescriptor( ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id), "r"); if (pfd != null) { try { final BitmapFactory.Options options = new BitmapFactory.Options(); // just decorde to get image size options.inJustDecodeBounds = true; BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor(), null, options); // calculate sub-sampling options.inSampleSize = calcSampleSize(options, requestWidth, requestHeight); options.inJustDecodeBounds = false; result = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor(), null, options); } finally { pfd.close(); } } return result; }