use of android.graphics.Bitmap in project ion by koush.
the class ResourceLoader method loadBitmap.
@Override
public Future<BitmapInfo> loadBitmap(final Context context, final Ion ion, final String key, final String uri, final int resizeWidth, final int resizeHeight, final boolean animateGif) {
if (uri == null || !uri.startsWith("android.resource:/"))
return null;
final SimpleFuture<BitmapInfo> ret = new SimpleFuture<BitmapInfo>();
// Log.d("FileLoader", "Loading file bitmap " + uri + " " + resizeWidth + "," + resizeHeight);
Ion.getBitmapLoadExecutorService().execute(new Runnable() {
@Override
public void run() {
try {
Resource res = lookupResource(context, uri);
BitmapFactory.Options options = ion.getBitmapCache().prepareBitmapOptions(res.res, res.id, resizeWidth, resizeHeight);
Point size = new Point(options.outWidth, options.outHeight);
BitmapInfo info;
if (animateGif && TextUtils.equals("image/gif", options.outMimeType)) {
InputStream in = res.res.openRawResource(res.id);
try {
info = loadGif(key, size, in, options);
} finally {
StreamUtility.closeQuietly(in);
}
} else {
Bitmap bitmap = IonBitmapCache.loadBitmap(res.res, res.id, options);
if (bitmap == null)
throw new Exception("Bitmap failed to load");
info = new BitmapInfo(key, options.outMimeType, bitmap, size);
}
info.servedFrom = ResponseServedFrom.LOADED_FROM_CACHE;
ret.setComplete(info);
} catch (OutOfMemoryError e) {
ret.setComplete(new Exception(e), null);
} catch (Exception e) {
ret.setComplete(e);
}
}
});
return ret;
}
use of android.graphics.Bitmap in project ion by koush.
the class StreamLoader method loadBitmap.
@Override
public Future<BitmapInfo> loadBitmap(final Context context, final Ion ion, final String key, final String uri, final int resizeWidth, final int resizeHeight, final boolean animateGif) {
final SimpleFuture<BitmapInfo> ret = new SimpleFuture<BitmapInfo>();
// Log.d("FileLoader", "Loading file bitmap " + uri + " " + resizeWidth + "," + resizeHeight);
Ion.getBitmapLoadExecutorService().execute(new Runnable() {
@Override
public void run() {
InputStream in = null;
try {
in = getInputStream(context, uri);
BitmapFactory.Options options = ion.getBitmapCache().prepareBitmapOptions(in, resizeWidth, resizeHeight);
StreamUtility.closeQuietly(in);
Point size = new Point(options.outWidth, options.outHeight);
BitmapInfo info;
in = getInputStream(context, uri);
if (animateGif && TextUtils.equals("image/gif", options.outMimeType)) {
info = loadGif(key, size, in, options);
} else {
Bitmap bitmap = IonBitmapCache.loadBitmap(in, options);
if (bitmap == null)
throw new Exception("Bitmap failed to load");
info = new BitmapInfo(key, options.outMimeType, bitmap, size);
}
info.servedFrom = ResponseServedFrom.LOADED_FROM_CACHE;
ret.setComplete(info);
} catch (OutOfMemoryError e) {
ret.setComplete(new Exception(e), null);
} catch (Exception e) {
ret.setComplete(e);
} finally {
StreamUtility.closeQuietly(in);
}
}
});
return ret;
}
use of android.graphics.Bitmap in project ion by koush.
the class VideoLoader method loadBitmap.
@Override
public Future<BitmapInfo> loadBitmap(Context context, Ion ion, final String key, final String uri, final int resizeWidth, final int resizeHeight, boolean animateGif) {
if (!uri.startsWith(ContentResolver.SCHEME_FILE))
return null;
final MediaFile.MediaFileType type = MediaFile.getFileType(uri);
if (type == null || !MediaFile.isVideoFileType(type.fileType))
return null;
final SimpleFuture<BitmapInfo> ret = new SimpleFuture<BitmapInfo>();
Ion.getBitmapLoadExecutorService().execute(new Runnable() {
@Override
public void run() {
final File file = new File(URI.create(uri));
if (ret.isCancelled()) {
// Log.d("VideoLoader", "Bitmap load cancelled (no longer needed)");
return;
}
try {
Bitmap bmp;
if (mustUseThumbnailUtils() || Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD_MR1)
bmp = ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), MediaStore.Video.Thumbnails.MINI_KIND);
else
bmp = createVideoThumbnail(file.getAbsolutePath());
if (bmp == null)
throw new Exception("video bitmap failed to load");
// downsample this if its obscenely large
Point originalSize = new Point(bmp.getWidth(), bmp.getHeight());
if (bmp.getWidth() > resizeWidth * 2 && bmp.getHeight() > resizeHeight * 2) {
float xratio = (float) resizeWidth / bmp.getWidth();
float yratio = (float) resizeHeight / bmp.getHeight();
float ratio = Math.min(xratio, yratio);
if (ratio != 0)
bmp = Bitmap.createScaledBitmap(bmp, (int) (bmp.getWidth() * ratio), (int) (bmp.getHeight() * ratio), true);
}
BitmapInfo info = new BitmapInfo(key, type.mimeType, bmp, originalSize);
info.servedFrom = ResponseServedFrom.LOADED_FROM_CACHE;
ret.setComplete(info);
} catch (OutOfMemoryError e) {
ret.setComplete(new Exception(e));
} catch (Exception e) {
ret.setComplete(e);
}
}
});
return ret;
}
use of android.graphics.Bitmap in project ion by koush.
the class AssetTests method testAsset.
public void testAsset() throws Exception {
Bitmap bitmap = Ion.with(getContext()).load("file:///android_asset/exif.jpg").asBitmap().get();
assertNotNull(bitmap);
}
use of android.graphics.Bitmap in project ion by koush.
the class IonBitmapCache method loadBitmap.
public static Bitmap loadBitmap(Resources res, int id, BitmapFactory.Options o) {
assert Thread.currentThread() != Looper.getMainLooper().getThread();
int rotation;
InputStream in = null;
try {
in = res.openRawResource(id);
byte[] bytes = new byte[50000];
int length = in.read(bytes);
rotation = Exif.getOrientation(bytes, 0, length);
} catch (Exception e) {
rotation = 0;
}
StreamUtility.closeQuietly(in);
Bitmap bitmap = BitmapFactory.decodeResource(res, id, o);
return getRotatedBitmap(bitmap, rotation);
}
Aggregations