I am getting a url and displaying it in Imageview. the auto roation of my device is ON. i want the imageview to scale according to width of device once it is rotated.
is it possible when I am getting image from a url?
You can do this in code as well if the image is adjusted in away that doesnt look right or you just want more control over what is happening on rotate.
First get the device width and height:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Then you can use that information to resize the image.
You can set o.inJustDecodeBounds = true to get the image sizes without loading the image. If the image is to big, you can resize it. Example code below.
private Bitmap getBitmap(String path) {
Uri uri = getImageUri(path);
InputStream in = null;
try {
final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
in = mContentResolver.openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > IMAGE_MAX_SIZE) {
scale++;
}
Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ", orig-height: " + o.outHeight);
Bitmap b = null;
in = mContentResolver.openInputStream(uri);
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeStream(in, null, o);
// resize to desired dimensions
int height = b.getHeight();
int width = b.getWidth();
Log.d(TAG, "1th scale operation dimenions - width: " + width + ", height: " + height);
double y = Math.sqrt(IMAGE_MAX_SIZE / (((double) width) / height));
double x = (y / height) * width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, (int) y, true);
b.recycle();
b = scaledBitmap;
System.gc();
} else {
b = BitmapFactory.decodeStream(in);
}
in.close();
Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " + b.getHeight());
return b;
} catch (IOException e) {
Log.e(TAG, e.getMessage(),e);
return null;
}
}
Related
Guess a picture of man. (Head at top & legs at bottom) :P
I am setting height dynamically using layoutparam.
When I set height for instance 300,
It sets height from HEAD to 300 units toward legs of image.
What I want is to set height from LEGs to top 300 units.
imageView.setHeight()
Use this method in your java class:
private void scaleImage(ImageView view) throws NoSuchElementException {
// Get bitmap from the the ImageView.
Bitmap bitmap = null;
try {
Drawable drawing = view.getDrawable();
bitmap = ((BitmapDrawable) drawing).getBitmap();
} catch (NullPointerException e) {
throw new NoSuchElementException("No drawable on given view");
} catch (ClassCastException e) {
// Check bitmap is Ion drawable
// bitmap = Ion.with(view).getBitmap();
}
// Get current dimensions AND the desired bounding box
int width = 0;
try {
width = bitmap.getWidth();
} catch (NullPointerException e) {
throw new NoSuchElementException("Can't find bitmap on given view/drawable");
}
int height = bitmap.getHeight();
int bounding = dpToPx(150);// set height
Logger.i("Test", "original width = " + Integer.toString(width));
Logger.i("Test", "original height = " + Integer.toString(height));
Logger.i("Test", "bounding = " + Integer.toString(bounding));
// Determine how much to scale: the dimension requiring less scaling is
// closer to the its side. This way the image always stays inside your
// bounding box AND either x/y axis touches it.
float xScale = ((float) bounding) / width;
float yScale = ((float) bounding) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
Logger.i("Test", "xScale = " + Float.toString(xScale));
Logger.i("Test", "yScale = " + Float.toString(yScale));
Logger.i("Test", "scale = " + Float.toString(scale));
// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
// Create a new bitmap and convert it to a format understood by the ImageView
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
width = scaledBitmap.getWidth(); // re-use
height = scaledBitmap.getHeight(); // re-use
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
Logger.i("Test", "scaled width = " + Integer.toString(width));
Logger.i("Test", "scaled height = " + Integer.toString(height));
// Apply the scaled bitmap
view.setImageDrawable(result);
// Now change ImageView's dimensions to match the scaled image
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
Logger.i("Test", "done");
}
private int dpToPx(int dp) {
float density = getApplicationContext().getResources().getDisplayMetrics().density;
return Math.round((float) dp * density);
}
Now, how to call this methord :
Bitmap bitmap;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
imageView.setImageBitmap(bitmap);
scaleImage(imageView);// call your imageview
} catch (IOException e) {
e.printStackTrace();
}
Can any one help to handle large bitmap taken by camera of 8MP.
I am facing error "Bitmap too large to be uploaded into a texture".
and what is the best way to compress image with respect to it's aspect ratio so it doesn't loose quality.
Thanks in advance.
change height and width with respect to the image ration dynamically and create a new bit map image
Bitmap bitmap = Bitmap.createScaledBitmap(capturedImage, width, height, true);
Try to add android:largeHeap="true" in the manifest
<application
...
android:hardwareAccelerated="false"
android:largeHeap="true" >
OR Try this method
private Bitmap getBitmap(String path) {
Uri uri = getImageUri(path);
InputStream in = null;
try {
final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
in = mContentResolver.openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) >
IMAGE_MAX_SIZE) {
scale++;
}
Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ",
orig-height: " + o.outHeight);
Bitmap b = null;
in = mContentResolver.openInputStream(uri);
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeStream(in, null, o);
// resize to desired dimensions
int height = b.getHeight();
int width = b.getWidth();
Log.d(TAG, "1th scale operation dimenions - width: " + width + ",
height: " + height);
double y = Math.sqrt(IMAGE_MAX_SIZE
/ (((double) width) / height));
double x = (y / height) * width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
(int) y, true);
b.recycle();
b = scaledBitmap;
System.gc();
} else {
b = BitmapFactory.decodeStream(in);
}
in.close();
Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " +
b.getHeight());
return b;
} catch (IOException e) {
Log.e(TAG, e.getMessage(),e);
return null;
}
I have an image view and I want to set an image on it. The size of the image is 7,707,446 bytes. Whenever I try to set the layout, the app crashes and the error is out of memory error.can anyone suggest me how to solve it.The xml is:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/image" >
</RelativeLayout>
</ScrollView>
Bitmap picture=BitmapFactory.decodeFile("/sdcard...");
int width = picture.getWidth();
int height = picture.getWidth();
float aspectRatio = (float) width / (float) height;
int newWidth = 70;
int newHeight = (int) (70 / aspectRatio);
picture= Bitmap.createScaledBitmap(picture, newWidth, newHeight, true);
public static Bitmap decodeImage(String arrayList_image) {
URL aURL;
try {
aURL = new URL(arrayList_image);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
return bm;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
See Loading Large Bitmaps Effectively.
You could create a function to calculate a downscaling factor:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
And then use this to load a scaled Bitmap:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
Thus if you wanted to load a 100x100 pixel thumbnail into your ImageView, you'd just use:
mImageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100)
);
Hi i suggest u to try this and display your image according the Device.
Bitmap bmp = BitmapFactory.decodeResource(myContext.getResources(),drawableId);
int w = bmp.getWidth();
int h = bmp.getHeight();
System.out.println("OrgWidth : " + w);
System.out.println("orgHeight : " + h);
int targetWidth = (screenWidth * w)/iPadWidth;
int targetHeight = (screenHeight * h)/iPadHeight;
System.out.println("TargetWidth : " + targetWidth);
System.out.println("TargetHeight : " + targetHeight);
int[] arrWidthHeight = {targetWidth,targetHeight};
// This for Slove the out of Memory problem
int newWidth = 110;
int newHeight = 110;
float scaleWidth = ((float) newWidth) / w;
float scaleHeight = ((float) newHeight) / h;
Matrix bMatrix = new Matrix();
bMatrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, w, h , bMatrix, true);
First fetch the Image from resources and find new height and width of image according to device and after scale this image as purpose for solve the out of memory here.
This for find height and width according to device height and width..
WindowManager winManager = (WindowManager) myContext.getSystemService(Context.WINDOW_SERVICE);
screenWidth = winManager.getDefaultDisplay().getWidth();
screenHeight = winManager.getDefaultDisplay().getHeight();
System.out.println("Screen Width : " + screenWidth);
System.out.println("Screen Height : " + screenHeight);
and use bitmap to get image height and width witch like a ipathwidth and ipathHeight ok now
Happy
How to get thumbnails from sdcard with the given URI ??
I have tried using bitmapfactory but the performance is bad or OutOfMemoryError
I am going to put the thumbnails to the listview with a lot of data
should I use thumbnails or any suggestions?
if thumbnails are to be used then how to do it...?
Thanks in advance for the help
The following method can be used to get the thumbnail of image:
private Bitmap getBitmap(String path) {
Uri uri = getImageUri(path);
InputStream in = null;
try {
final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
in = mContentResolver.openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > IMAGE_MAX_SIZE) {
scale++;
}
Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ", orig-height: " + o.outHeight);
Bitmap b = null;
in = mContentResolver.openInputStream(uri);
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeStream(in, null, o);
// resize to desired dimensions
int height = b.getHeight();
int width = b.getWidth();
Log.d(TAG, "1th scale operation dimenions - width: " + width + ", height: " + height);
double y = Math.sqrt(IMAGE_MAX_SIZE
/ (((double) width) / height));
double x = (y / height) * width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, (int) y, true);
b.recycle();
b = scaledBitmap;
System.gc();
} else {
b = BitmapFactory.decodeStream(in);
}
in.close();
Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " + b.getHeight());
return b;
} catch (IOException e) {
Log.e(TAG, e.getMessage(),e);
return null;
}
}
And always call bitmap.recycle() method after using bitmaps. It will clear the bitmap from memory. Also avoid memory leaks in your code. This will solve your OOME.
// Parameters
int w,h;
// First only decode image size
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(file, opt);
// Decode small enough image
int heightRatio = (int)opt.outHeight/h;
int widthRatio = (int)opt.outWidth/w;
if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio)
opt.inSampleSize = heightRatio;
else
opt.inSampleSize = widthRatio;
}
opt.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(file, opt);
Use ThumbnailUtils class to get the thumb of the image/video.
Try with this code, it fix the Out of memory error. Change the height and width as yours. Here intent_data2 is file path.
public Bitmap custom_SizedImage(String intent_data2) {
int targetHeight = 534, targetWidth = 534;
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(intent_data2, options);
double sampleSize = 0;
Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
.abs(options.outWidth - targetWidth);
if (options.outHeight * options.outWidth * 2 >= 1638) {
sampleSize = scaleByHeight ? options.outHeight / targetHeight
: options.outWidth / targetWidth;
sampleSize = (int) Math.pow(2d,
Math.floor(Math.log(sampleSize) / Math.log(2d)));
}
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[128];
while (true) {
try {
options.inSampleSize = (int) sampleSize;
mBitmap = BitmapFactory.decodeFile(intent_data2, options);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int screenDiff = height - width;
int screenRatio = screenDiff / 3;
float scaleFactor = mBitmap.getWidth() / width;
float y_Pos = scaleFactor * screenRatio;
Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);
croppedBitmap = Bitmap.createBitmap(mBitmap, 0, (int) y_Pos,
mBitmap.getWidth(), mBitmap.getWidth(), matrix, true);
scaledBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
Config.RGB_565);
float ratioX = targetWidth / (float) croppedBitmap.getWidth();
float ratioY = targetHeight / (float) croppedBitmap.getHeight();
float middleX = targetWidth / 2.0f;
float middleY = targetHeight / 2.0f;
Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
Canvas canvas = new Canvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(croppedBitmap,
middleX - croppedBitmap.getWidth() / 2, middleY
- croppedBitmap.getHeight() / 2, new Paint(
Paint.FILTER_BITMAP_FLAG));
//saveImage(scaledBitmap);
break;
} catch (Exception ex) {
try {
sampleSize = sampleSize * 2;
} catch (Exception ex1) {
}
}
}
return scaledBitmap;
}
I'm getting a photo from sd starting a new intent how this:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
This work great! but in some cases, image size is too large and mobile device crashes. If is a normal size image not problem, but I wanna a way to filter or avoid the larger size images.
Try using this
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"), 0);
hope it will help, it works for me.
I found the solution to this problem, I use the next method to get the bitmap resized using Bitmaps options. You can set the max size (actually 1.2MP) but it's great result there.
private Bitmap getBitmap(String path) {
Uri uri = getImageUri(path);
InputStream in = null;
try {
final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
in = mContentResolver.openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > IMAGE_MAX_SIZE) {
scale++;
}
Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ", orig-height: " + o.outHeight);
Bitmap b = null;
in = mContentResolver.openInputStream(uri);
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeStream(in, null, o);
// resize to desired dimensions
int height = b.getHeight();
int width = b.getWidth();
Log.d(TAG, "1th scale operation dimenions - width: " + width + ", height: " + height);
double y = Math.sqrt(IMAGE_MAX_SIZE
/ (((double) width) / height));
double x = (y / height) * width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, (int) y, true);
b.recycle();
b = scaledBitmap;
System.gc();
} else {
b = BitmapFactory.decodeStream(in);
}
in.close();
Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " + b.getHeight());
return b;
} catch (IOException e) {
Log.e(TAG, e.getMessage(),e);
return null;
}
}