Java Code Examples for android.graphics.BitmapFactory#decodeFileDescriptor()

The following examples show how to use android.graphics.BitmapFactory#decodeFileDescriptor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BitmapDecoder.java    From android-open-project-demo with Apache License 2.0 6 votes vote down vote up
public static Bitmap decodeSampledBitmapFromDescriptor(FileDescriptor fileDescriptor, BitmapSize maxSize, Bitmap.Config config) {
    synchronized (lock) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true; // 只读头信息
        options.inPurgeable = true;
        options.inInputShareable = true;
        BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
        //这个就是图片压缩倍数的参数
        options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight());
        options.inJustDecodeBounds = false;
        if (config != null) {
            options.inPreferredConfig = config;
        }
        try {
            return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
        } catch (Throwable e) {
            LogUtils.e(e.getMessage(), e);
            return null;
        }
    }
}
 
Example 2
Source File: ImageResizer.java    From BubbleCloudView with MIT License 6 votes vote down vote up
/**
 * 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
 * @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
 */
public static Bitmap decodeSampledBitmapFromDescriptor(
        FileDescriptor fileDescriptor, int reqWidth, int reqHeight, ImageCache cache) {

    // 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;

    // If we're running on Honeycomb or newer, try to use inBitmap
    if (Utils.hasHoneycomb()) {
        addInBitmapOptions(options, cache);
    }

    return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
}
 
Example 3
Source File: BitmapHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * ファイルからビットマップを読み込んで指定した幅・高さに最も近い大きさのBitmapとして返す
 * @param fd
 * @param requestWidth
 * @param requestHeight
 * @return
 */
@Nullable
public static Bitmap asBitmap(
	final FileDescriptor fd,
	final int requestWidth, final int requestHeight) {

	Bitmap bitmap = null;
	if (fd != null && fd.valid()) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;	// Bitmapを生成せずにサイズ等の情報だけを取得する
		BitmapFactory.decodeFileDescriptor(fd, null, options);
		options.inJustDecodeBounds = false;
		options.inSampleSize = calcSampleSize(options, requestWidth, requestHeight);
		bitmap = BitmapFactory.decodeFileDescriptor(fd, null, options);
		final int orientation = getOrientation(fd);
		if (orientation != 0) {
			final Bitmap newBitmap = rotateBitmap(bitmap, orientation);
			bitmap.recycle();
			bitmap = newBitmap;
		}
	}
	return bitmap;
}
 
Example 4
Source File: BitmapManager.java    From memoir with Apache License 2.0 6 votes vote down vote up
/**
 * The real place to delegate bitmap decoding to BitmapFactory.
 */
public Bitmap decodeFileDescriptor(FileDescriptor fd, BitmapFactory.Options options) {
    if (options.mCancel) {
        return null;
    }

    Thread thread = Thread.currentThread();
    if (!canThreadDecoding(thread)) {
        return null;
    }

    setDecodingOptions(thread, options);
    Bitmap b = BitmapFactory.decodeFileDescriptor(fd, null, options);

    removeDecodingOptions(thread);
    return b;
}
 
Example 5
Source File: ImagePicker.java    From QuickerAndroid with GNU General Public License v3.0 6 votes vote down vote up
private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = sampleSize;

    AssetFileDescriptor fileDescriptor = null;
    try {
        fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    Bitmap actuallyUsableBitmap = BitmapFactory.decodeFileDescriptor(
            fileDescriptor.getFileDescriptor(), null, options);

    Log.d(TAG, options.inSampleSize + " sample method bitmap ... " +
            actuallyUsableBitmap.getWidth() + " " + actuallyUsableBitmap.getHeight());

    return actuallyUsableBitmap;
}
 
Example 6
Source File: BitmapManager.java    From memoir with Apache License 2.0 6 votes vote down vote up
/**
 * The real place to delegate bitmap decoding to BitmapFactory.
 */
public Bitmap decodeFileDescriptor(FileDescriptor fd, BitmapFactory.Options options) {
    if (options.mCancel) {
        return null;
    }

    Thread thread = Thread.currentThread();
    if (!canThreadDecoding(thread)) {
        return null;
    }

    setDecodingOptions(thread, options);
    Bitmap b = BitmapFactory.decodeFileDescriptor(fd, null, options);

    removeDecodingOptions(thread);
    return b;
}
 
Example 7
Source File: BitmapManager.java    From reader with MIT License 6 votes vote down vote up
/**
 * The real place to delegate bitmap decoding to BitmapFactory.
 */
public Bitmap decodeFileDescriptor(FileDescriptor fd,
                                   BitmapFactory.Options options) {
    if (options.mCancel) {
        return null;
    }

    Thread thread = Thread.currentThread();
    if (!canThreadDecoding(thread)) {
        Log.d(TAG, "Thread " + thread + " is not allowed to decode.");
        return null;
    }

    setDecodingOptions(thread, options);
    Bitmap b = BitmapFactory.decodeFileDescriptor(fd, null, options);

    removeDecodingOptions(thread);
    return b;
}
 
Example 8
Source File: BitmapUtils.java    From AlbumCameraRecorder with MIT License 6 votes vote down vote up
/**
 * 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
 */
public static Bitmap decodeBitmapFromDescriptor(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);
}
 
Example 9
Source File: ContactImageUtil.java    From ListViewVariants with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 10
Source File: BitmapUtil.java    From TvLauncher with Apache License 2.0 5 votes vote down vote up
/**
 * get Bitmap
 *
 * @param imgFile
 * @param minSideLength
 * @param maxNumOfPixels
 * @return
 */
public static Bitmap tryGetBitmap(String imgFile, int minSideLength, int maxNumOfPixels) {
    if (imgFile == null || imgFile.length() == 0)
        return null;

    try {
        FileInputStream fi = new FileInputStream(imgFile);
        FileDescriptor fd = fi.getFD();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeFileDescriptor(fd, null, options);

        options.inSampleSize = computeSampleSize(options, minSideLength, maxNumOfPixels);
        try {
            // 这里一定要将其设置回false,因为之前我们将其设置成了true
            // 设置inJustDecodeBounds为true后,decodeFile并不分配空间,即,BitmapFactory解码出来的Bitmap为Null,但可计算出原始图片的长度和宽度
            options.inJustDecodeBounds = false;

            Bitmap bmp = BitmapFactory.decodeFile(imgFile, options);
            fi.close();
            return bmp == null ? null : bmp;
        } catch (OutOfMemoryError err) {
            System.gc();
            System.runFinalization();
            return null;
        }
    } catch (Exception e) {
        return null;
    }
}
 
Example 11
Source File: BitmapDecoder.java    From android-open-project-demo with Apache License 2.0 5 votes vote down vote up
public static Bitmap decodeFileDescriptor(FileDescriptor fileDescriptor) {
    synchronized (lock) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPurgeable = true;
        options.inInputShareable = true;
        try {
            return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
        } catch (Throwable e) {
            LogUtils.e(e.getMessage(), e);
            return null;
        }
    }
}
 
Example 12
Source File: BitmapUtils.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Given a FileDescriptor, decodes the contents and returns a bitmap of
 * dimensions |size|x|size|.
 * @param descriptor The FileDescriptor for the file to read.
 * @param size The width and height of the bitmap to return.
 * @return The resulting bitmap.
 */
public static Bitmap decodeBitmapFromFileDescriptor(FileDescriptor descriptor, int size) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(descriptor, null, options);
    options.inSampleSize = calculateInSampleSize(options.outWidth, options.outHeight, size);
    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeFileDescriptor(descriptor, null, options);

    if (bitmap == null) return null;

    return sizeBitmap(bitmap, size);
}
 
Example 13
Source File: ImagePicker.java    From QuickerAndroid with GNU General Public License v3.0 5 votes vote down vote up
public static int calculateInSampleSize(Context context, Uri selectedImage, int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    AssetFileDescriptor fileDescriptor = null;
    try {
        fileDescriptor = context.getContentResolver().openAssetFileDescriptor(selectedImage, "r");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    BitmapFactory.decodeFileDescriptor(
            fileDescriptor.getFileDescriptor(), null, options);

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {

        final int halfHeght = height / 2;
        final int halfWidth = width / 2;

        while ((halfHeght / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }

    }
    return inSampleSize;
}
 
Example 14
Source File: Utility.java    From Shelter with Do What The F*ck You Want To Public License 5 votes vote down vote up
public static Bitmap decodeSampledBitmap(FileDescriptor fd,
                                         int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fd, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFileDescriptor(fd, null, options);
}
 
Example 15
Source File: ImageMetadata.java    From Onosendai with Apache License 2.0 5 votes vote down vote up
private void readDimentions () throws IOException {
	synchronized (this.bitmapLock) {
		final Options opts = new Options();
		opts.inJustDecodeBounds = true;
		BitmapFactory.decodeFileDescriptor(openFileDescriptor().getFileDescriptor(), null, opts);
		this.cachedWidth = opts.outWidth;
		this.cachedHeight = opts.outHeight;
	}
}
 
Example 16
Source File: ImageUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取 Bitmap
 * @param fd        文件描述
 * @param maxWidth  最大宽度
 * @param maxHeight 最大高度
 * @return {@link Bitmap}
 */
public static Bitmap getBitmap(final FileDescriptor fd, final int maxWidth, final int maxHeight) {
    if (fd == null) return null;
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(fd, null, options);
        options.inSampleSize = BitmapUtils.calculateInSampleSize(options, maxWidth, maxHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFileDescriptor(fd, null, options);
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getBitmap");
        return null;
    }
}
 
Example 17
Source File: ImageUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取 Bitmap
 * @param fd      文件描述
 * @param options {@link BitmapFactory.Options}
 * @return {@link Bitmap}
 */
public static Bitmap decodeFileDescriptor(final FileDescriptor fd, final BitmapFactory.Options options) {
    if (fd == null) return null;
    try {
        return BitmapFactory.decodeFileDescriptor(fd, null, options);
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "decodeFileDescriptor");
        return null;
    }
}
 
Example 18
Source File: BitmapDecoder.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
public static Bitmap decodeSampledBitmapFromDescriptor(FileDescriptor fileDescriptor, int reqWidth, int reqHeight) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPurgeable = true;
        BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        try {
        	 return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
		} catch (OutOfMemoryError e) {
			 e.printStackTrace();
			 return null;
		}
    }
 
Example 19
Source File: ImageResizer.java    From ClassSchedule with Apache License 2.0 5 votes vote down vote up
/**
 * 压缩加载图片 从文件<br>
 */
public static Bitmap decodeSampledBitmapFromDescriptor(FileDescriptor descriptor, int reqWidth, int reqHeight) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(descriptor, null, options);
    int inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    options.inSampleSize = inSampleSize;
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFileDescriptor(descriptor, null, options);
}
 
Example 20
Source File: BitmapUtils.java    From FastAndroid with Apache License 2.0 2 votes vote down vote up
/**
 * 获取 bitmap
 *
 * @param fd 文件描述
 * @return bitmap
 */
public static Bitmap getBitmap(final FileDescriptor fd) {
    if (fd == null) return null;
    return BitmapFactory.decodeFileDescriptor(fd);
}