下面列出了android.graphics.BitmapFactory#decodeFileDescriptor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
}
/**
* 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);
}
/**
* ファイルからビットマップを読み込んで指定した幅・高さに最も近い大きさの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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
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);
}
/**
* 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);
}
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;
}
/**
* 压缩加载图片 从文件<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);
}
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;
}
}
/**
* 获取 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;
}
}
/**
* 获取 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;
}
}
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;
}
}
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);
}
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;
}
/**
* 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);
}
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;
}
}
}
/**
* 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;
}
}
/**
* 获取 bitmap
*
* @param fd 文件描述
* @return bitmap
*/
public static Bitmap getBitmap(final FileDescriptor fd) {
if (fd == null) return null;
return BitmapFactory.decodeFileDescriptor(fd);
}