I've tested this code snippet on about 25 devices and it works great on all of them except a Samsung Galaxy Nexus that I'm trying to test with now.
Here is the method and I apologize for not trimming it down to find the exact spot that's throwing the exception, but eclipse's debugging is doodoo.
private void setupImageView() {
imageLocation = currentPhotoPath;
// Get the dimensions of the View
Display display = getWindowManager().getDefaultDisplay();
Point size = getDisplaySize(display);
int targetW = size.x;
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageLocation, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW / targetW, photoH / targetW);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(imageLocation, bmOptions);
//int rotationForImage = getRotationForImage(imageLocation);
int rotationForImage = (whichCamera == 0 ? 90 : 270);
if (rotationForImage != 0) {
int targetWidth = rotationForImage == 90 || rotationForImage == 270 ? bitmap.getHeight() : bitmap.getWidth();
int targetHeight = rotationForImage == 90 || rotationForImage == 270 ? bitmap.getWidth() : bitmap.getHeight();
Bitmap rotatedBitmap = Bitmap.createBitmap(targetWidth, targetHeight, bitmap.getConfig());
Canvas canvas = new Canvas(rotatedBitmap);
Matrix matrix = new Matrix();
matrix.setRotate(rotationForImage, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
canvas.drawBitmap(bitmap, matrix, new Paint());
bitmap.recycle();
bitmap = rotatedBitmap;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
try
{
File f = new File(imageLocation);
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
}
catch(java.io.IOException e){}
}
imageView.setImageBitmap(bitmap);
}
anyone know what Samsung does differently with the nexus that would cause this to throw an exception? It works fine on a Galaxy S III
It looks like something in the if block you mention is throwing an NPE - that's the real bug here. Don't worry about the Activity/ResultInfo stuff, that is downstream and triggered by the NPE. Go line by line and look for the null reference :-)
Regarding Eclipse - sadly I don't have much experience there. For Android I personally use IntelliJ and the debugging works well. Are you able to debug other Java code (even a simple Hello, World)?
Related
I use this function to reduce the size of image before uploading it,But using below method my file size is increasing
Before use below code my file size---> 157684
after using this code my file size ----->177435
Can some one help me please how can i reduce file size before upload to server
code:
public File saveBitmapToFile(File file){
try {
// BitmapFactory options to downsize the image
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inSampleSize = 6;
// factor of downsizing the image
FileInputStream inputStream = new FileInputStream(file);
//Bitmap selectedBitmap = null;
BitmapFactory.decodeStream(inputStream, null, o);
inputStream.close();
// The new size we want to scale to
final int REQUIRED_SIZE=75;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&
o.outHeight / scale / 2 >= REQUIRED_SIZE) {
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
inputStream = new FileInputStream(file);
Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, o2);
inputStream.close();
// here i override the original image file
file.createNewFile();
FileOutputStream outputStream = new FileOutputStream(file);
selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100 , outputStream);
return file;
} catch (Exception e) {
return null;
}
}
This what I use to reduce my image size without compressing :
public static Bitmap getResizedBitmap(Bitmap bitmap, int newWidth, int newHeight) {
Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
float ratioX = newWidth / (float) bitmap.getWidth();
float ratioY = newHeight / (float) bitmap.getHeight();
float middleX = newWidth / 2.0f;
float middleY = newHeight / 2.0f;
Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
Canvas canvas = new Canvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
You just need to enter correct new height and width to fit your needs
We want to make thumbnail of an image, so we need to first take the ByteArrayOutputStream and then pass it into Bitmap.compress() method.
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
youBitmapImage.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
more about the function, from the docs
If your output file is larger:
it can mean that scale is wrong. And you save the file with 100% quality so it can grow
compression on the input file is extremely heavy and even though you scale it, using no compression on the output still generates a larger file
Change this line:
selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100 , outputStream);
to
int mCompressedSize = 50; // 0 is lowest and 100 original
selectedBitmap.compress(Bitmap.CompressFormat.PNG, mCompressedSize, outputStream);
Hope this will help.
Try
int compressionRatio = 2; //1 == originalImage, 2 = 50% compression, 4=25% compress
File file = new File (imageUrl);
try {
Bitmap bitmap = BitmapFactory.decodeFile (file.getPath ());
bitmap.compress (Bitmap.CompressFormat.JPEG, compressionRatio, new FileOutputStream (file));
}
catch (Throwable t) {
Log.e("ERROR", "Error compressing file." + t.toString ());
t.printStackTrace ();
}
I have two images and I want to set one upon another.
However after merging I get low quality file.
This is my original topImage (96x96) pixels:
This is my bottomIamge (74x74) pixels:
You can see that quality is pretty good.
When I run under mentioned code I get merged Image (74x74):
Now you can see that topImage lost his quality.
Here is relevant code:
// load bottom image from assets:
InputStream is;
Bitmap bottomImage;
try {
is = context.getAssets().open("images/avatar1.png");
} catch (IOException e1) {
int resID = context.getResources().getIdentifier("unknown_item", "drawable", context.getPackageName());
is = context.getResources().openRawResource(resID);
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
bottomImage = BitmapFactory.decodeStream(is,null,options);
try {
is.close();
is = null;
} catch (IOException e) {
}
Bitmap topImage = null;
String base64Img = null;
// get byte array from String (base64).
byte[] backToBytes = Base64.decode(base64Img, Base64.DEFAULT);
// here I verified that image I got from byte array still has good quality
//writeToStorage(backToBytes, "test.png");
// create Bitmap
topImage = BitmapFactory.decodeByteArray(backToBytes, 0, backToBytes.length, null);
// scale the image
topImage = Bitmap.createScaledBitmap(topImage, 74, 74, false); // set fixed size 74x74 image
topImage = Bitmap.createBitmap(topImage, topImage.getWidth()/4, 0, topImage.getWidth()/2, topImage.getHeight());
// shift it:
Canvas comboImage = new Canvas(bottomImage);
// Then draw the second on top of that
comboImage.drawBitmap(topImage, 0f, 0f, null);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bottomImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
String base64String = Base64.encodeToString(byteArray, Base64.DEFAULT);
If I'll draw base64String I get merged Image
Do I miss something?
Does Bitmap.createScaledBitmap(topImage, 37, 74, false); scales to 37x74 pixels?
Thanks,
For now I use this way (based on THIS answer):
// this method should replace Bitmap.createScaledBitmap
private Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight){
Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
float ratioX = newWidth / (float) bitmap.getWidth();
float ratioY = newHeight / (float) bitmap.getHeight();
float middleX = newWidth / 2.0f;
float middleY = newHeight / 2.0f;
Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
Canvas canvas = new Canvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
And now I can replace:
topImage = Bitmap.createScaledBitmap(topImage, 74, 74, false);
with:
topImage = scaleBitmap(topImage, 74, 74);
was:
now:
Its not the same quality but seems better
You cannot keep the quality when scaling by not hall number(2,3,4,5). In your case you are scaling 96 pixels to 74.
You can either change the scale to 48 pixels(x2) or keep it 96 pixels.
Also watch this video that explain what is anti-aliacing, and how it can help you.
In the app I'm working on, part of the user's input is a series of images. Some of these might be 4MB large in their raw form. I resize and rotate them, then save them in the app's portion of the device memory for later use. The problem I'm experiencing is that I seem to run out of memory even though I recycle each Bitmap after it's saved.
Here's the main processing
private class SaveImagesTask extends AsyncTask<Long, Void, Void>{
#Override
protected Void doInBackground(Long... ids){
long id = ids[0];
Iterator<ImageButton> itImg = arrBtnImage.iterator();
Iterator<TextView> itLbl = arrLblImage.iterator();
while(itImg.hasNext() && itLbl.hasNext()){
String imgPath = (String) itImg.next().getTag();
String imgLbl = itLbl.next().getText().toString().trim();
String imgName = imgLbl.replace(" ", "_").replace(",", "_");
imgName += ".jpg";
if(imgPath != null){
/* Save resized version of image */
File dir = getApplicationContext().getFilesDir();
dir = new File(dir, "temp/" + Long.toString(plantId));
boolean madeDir = dir.mkdirs();
File path = new File(dir, imgName);
Bitmap toSave = getScaledBitmap(imgPath, IMAGE_MAX_SIDE_LENGTH, IMAGE_MAX_SIDE_LENGTH);
try{
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(path));
boolean insertSuccess = toSave.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
outStream.close();
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
toSave.recycle();
}//if
}//while(more images to process)
}// method: doInBackground(params)
}// inner class: saveImages extends AsyncTask
And here's where I resize the image
private Bitmap getScaledBitmap(String picturePath, int newWidth, int newHeight){
/* Size */
BitmapFactory.Options sizeOptions = new BitmapFactory.Options();
sizeOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(picturePath, sizeOptions);
int sampleSize = 1;
int rawHeight = sizeOptions.outHeight;
int rawWidth = sizeOptions.outWidth;
if(rawHeight > newHeight || rawWidth > newWidth){
/* Find the dimension that needs to change the most */
int heightRatio = Math.round((float) rawHeight / (float) newHeight);
int widthRatio = Math.round((float) rawWidth / (float) newWidth);
sampleSize = (heightRatio > widthRatio ? heightRatio : widthRatio);
}//if(raw image is wider or taller than it should be){reduce size so neither is too large}
sizeOptions.inJustDecodeBounds = false;//Load pixels for display.
sizeOptions.inSampleSize = sampleSize;//Set shrink factor.
Bitmap scaledBitmap = BitmapFactory.decodeFile(picturePath, sizeOptions);
/* Rotation */
int rotation = 1;
try{
ExifInterface exif = new ExifInterface(picturePath);
rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
}
catch(IOException e){
e.printStackTrace();
}
int rotationInDegrees = 0;
if(rotation == ExifInterface.ORIENTATION_ROTATE_90)
rotationInDegrees = 90;
else if(rotation == ExifInterface.ORIENTATION_ROTATE_180)
rotationInDegrees = 180;
else if(rotation == ExifInterface.ORIENTATION_ROTATE_270)
rotationInDegrees = 270;
Matrix matrix = new Matrix();
if(rotation != 0f)
matrix.preRotate(rotationInDegrees);
return Bitmap.createBitmap(scaledBitmap, 0, 0,
scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
}// method: getScaledBitmap(String, int, int)
Before I start getting comments about this being so common of a question, I'll point out that I'm not displaying these images, so it's not like I'm trying to keep all of these in memory. I need to keep large images because users will want to be able to zoom in on the pictures, but I'm resizing them because they don't need to be ridiculously huge. Pretty much any other solution I've seen on SO for images and OOM errors don't apply to my back-to-back access of multiple images.
So like I said, I'm recycling each Bitmap after it's saved, but they still seem to be using memory. Any idea what I'm missing?
You're not recycling scaledBitmap in getScaledBitmap. Fixing that should help. Change this line:
return Bitmap.createBitmap(scaledBitmap, 0, 0,
scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
to something like:
Bitmap newBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
scaledBitmap.recycle();
return newBitmap;
If you have multiple threads working on large bitmaps, you will use a lot of memory on some cases.
What you need is to find the best approach according to your needs. here are some things you can do and/or need to know:
use a single thread for the images handling.
always recycle old bitmaps that you don't need anymore, as soon as possible. it's true that the GC will help you, but that can help it too, and it will work even on pre-honeycomb devices.
do the image manipulations via NDK (so you won't need to have 2 bitmaps for each image manipulation), for example using this.
downsample the image to the minimal size that you need, and never assume that the memory is large enough for any given image (unless you are 100% sure that the images are small).
remember that the requirements for android devices are still very low in terms of RAM per app (heap size) - the bare minimal is still 16MB per app.
you can use android:largeHeap="true" in the manifest, but that doesn't mean anything about how much more you will get, if at all.
I am trying to send an image to a server. Before sending it, I am reducing its size and quality, and then fixing any rotation issue. My problem is that, after rotating the image, when I save it, the file is bigger than before. Before rotation size was 10092 and after rotation is 54226
// Scale image to reduce it
Bitmap reducedImage = reduceImage(tempPhotoPath);
// Decrease photo quality
FileOutputStream fos = new FileOutputStream(tempPhotoFile);
reducedImage.compress(CompressFormat.JPEG, 55, fos);
fos.flush();
fos.close();
// Check and fix rotation issues
Bitmap fixed = fixRotation(tempPhotoPath);
if(fixed!=null)
{
FileOutputStream fos2 = new FileOutputStream(tempPhotoFile);
fixed.compress(CompressFormat.JPEG, 100, fos2);
fos2.flush();
fos2.close();
}
public Bitmap reduceImage(String originalPath)
{
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inPurgeable = true;
o.inInputShareable = true;
BitmapFactory.decodeFile(originalPath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 320;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inPurgeable = true;
o2.inInputShareable = true;
o2.inSampleSize = scale;
Bitmap bitmapScaled = null;
bitmapScaled = BitmapFactory.decodeFile(originalPath, o2);
return bitmapScaled;
}
public Bitmap fixRotation(String path)
{
Bitmap b = null;
try
{
//Find if the picture is rotated
ExifInterface exif = new ExifInterface(path);
int degrees = 0;
if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6"))
degrees = 90;
else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8"))
degrees = 270;
else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3"))
degrees = 180;
if(degrees > 0)
{
BitmapFactory.Options o = new BitmapFactory.Options();
o.inPurgeable = true;
o.inInputShareable = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, o);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(degrees);
b = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
}
catch(Exception e){e.printStackTrace();}
return b;
}
You're compressing it with different quality measures. After rotation, you're using quality 100, so it's going to be a larger file than the previous one, with quality 55.
When you compress an image, it doesn't matter what the current file size/quality is. That has no real impact on the outcome. Compressing at 55 quality, followed by 100 quality, does not result in a file with the same size as a simple 55 quality compression. It results in a file with the size of 100 quality compression, because that's the last thing done to it.
For your specific code, I'm not sure I see the reason behind compressing it twice anyway. Compression(file size) isn't what was causing your OOM problems when rotating, the image dimensions were most likely the culprit. Shrinking the image before rotating should fix that, no need for saving a temp file.
All you need to do is run reduceImage(), then follow it up with fixRotation(). Fix your rotation method so that it accepts a Bitmap instead of a path, so you don't need to save the file in between. Finally, save/compress it at whatever quality you desire.
If you do need the temp file for some reason, use PNG for the first compression. This way it's lossless, so when you recompress the final image, you won't have used JPG(lossy) twice at a low quality.
I am working on an application, in which I need to pick an image from sd card and show it in image view. Now I want the user to decrease/increase its width by clicking a button and then save it back to the sd card.
I have done the image picking and showing it on ui. But unable to find how to resize it.Can anyone please suggest me how to achieve it.
Just yesterday i have done this
File dir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
Bitmap b= BitmapFactory.decodeFile(PATH_ORIGINAL_IMAGE);
Bitmap out = Bitmap.createScaledBitmap(b, 320, 480, false);
File file = new File(dir, "resize.png");
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
out.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
b.recycle();
out.recycle();
} catch (Exception e) {}
Also don't forget to recycle your bitmaps: It will save memory.
You can also get path of new created file String: newPath=file.getAbsolutePath();
Solution without OutOfMemoryException in Kotlin
fun resizeImage(file: File, scaleTo: Int = 1024) {
val bmOptions = BitmapFactory.Options()
bmOptions.inJustDecodeBounds = true
BitmapFactory.decodeFile(file.absolutePath, bmOptions)
val photoW = bmOptions.outWidth
val photoH = bmOptions.outHeight
// Determine how much to scale down the image
val scaleFactor = Math.min(photoW / scaleTo, photoH / scaleTo)
bmOptions.inJustDecodeBounds = false
bmOptions.inSampleSize = scaleFactor
val resized = BitmapFactory.decodeFile(file.absolutePath, bmOptions) ?: return
file.outputStream().use {
resized.compress(Bitmap.CompressFormat.JPEG, 75, it)
resized.recycle()
}
}
Try using this method:
public static Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) {
if(bitmapToScale == null)
return null;
//get the original width and height
int width = bitmapToScale.getWidth();
int height = bitmapToScale.getHeight();
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(newWidth / width, newHeight / height);
// recreate the new Bitmap and set it back
return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);
}
You can use Bitmap.createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter)
I found this useful library for this achievement: https://github.com/hkk595/Resizer
Here my static method:
public static void resizeImageFile(File originalImageFile, File resizedImageFile, int maxSize) {
Bitmap bitmap = BitmapFactory.decodeFile(originalImageFile.getAbsolutePath());
Bitmap resizedBitmap;
if (bitmap.getWidth() > bitmap.getHeight()) {
resizedBitmap = Bitmap.createScaledBitmap(bitmap, maxSize, maxSize * bitmap.getHeight() / bitmap.getWidth(), false);
} else {
resizedBitmap = Bitmap.createScaledBitmap(bitmap, maxSize * bitmap.getWidth() / bitmap.getHeight(), maxSize, false);
}
FileOutputStream fileOutputStream;
try {
fileOutputStream = new FileOutputStream(resizedImageFile);
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
bitmap.recycle();
resizedBitmap.recycle();
} catch (Exception e) {
e.printStackTrace();
}
}
You should ideally use multitouch instead of using a button to increase/decrease width. Here's an amazing library. Once the user decides to save the image, the image translation matrix must be stored persistently (in your sqlite database). Next time the user opens the image, you need to recall the matrix and apply it to your image.
I've actually done this before.