I have a problem in saving images in my app.
The process is simple:
1 - I read the image into a bitmap for display
2 - Save the bitmap as a new file
At this point, I do not understand why the final file has a size much larger than the original image. For example, a 170 kb image is saved in a new file 1 mb.
I know the difference in the management of compression between PNG and JPG, but I expect that if I read a PNG file, and save it in the same format, the size remains the same.
Thank you all for the help.
Edit
This is my code for saving method:
Bitmap resized = Bitmap.createScaledBitmap(original, generateWidth, generateHeight, false);
FileOutputStream out = new FileOutputStream(tempResizedFile.getPath());
resized.compress(generateType.CompressFormat(),
qualityBar.getProgress(), out);
out.close();
Related
In the app I am developing, I have an activity in which I show two ImageViews made of two bitmaps, ca.bm and ca.tempBm respectively. It works fine on my device but produces OutOfMemoryError on some devices when trying to copy the original bitmap to the bitmap I will modify later:
ca.tempBm = Bitmap.createBitmap(ca.bm); // out of memory error
I would like to create a temp file instead to hold the original image and work with a single bitmap, but the temp file must be preserved when changing from the Camera Activity to the next activity and should be destroyed only when the second activity does.
How can I achieve this?
You can write a Bitmap to the cache directory with:
FileOutputStream outStream = new FileOutputStream(new File(getCacheDir(), "tempBMP"));
myBitmap.compress(Bitmap.CompressFormat.JPEG, 75, outStream);
outStream.close();
And read back in with:
Bitmap myBitmap = BitmapFactory.decodeFile(bitmapPath.getAbsolutePath());
But you might be better off just saving memory by creating a smaller Bitmap, drawing the larger Bitmap to the smaller Bitmap's canvas, then passing around the smaller Bitmap between Activities.
I use the following code to obtain a bitmap from an ImageView. This image is not saved anywhere else on my device. I want to upload this image into an online mysqli database. However, to do so I need to decrease the size of the file first. I found a lot of links about this, however they all require the file to be saved on the device and then use FileOutputStream. I am looking for a way to reduce the file size so that it can be comfortably transferred using the Volley API ( i am currently receiving either run out of memory exceptions or broken pipe errors). Hence I am looking for a way to modify this code to be able to significantly decrease the file size, whilst still maintaining a quality which can be comfortably shown on a mobile device. The original image is taken straight from the camera hence the size is quite large. Here is my code:
ImageView pic_holder = (ImageView) findViewById(R.id.picturedisplay);
Bitmap bitmap = ((BitmapDrawable)pic_holder.getDrawable()).getBitmap();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();
String img_str = Base64.encodeToString(image, 0);
I would like to decrease the size of the img_str which is passed to my Volley method.
I am trying to write a method in android to save the whole webview as an image as following:
bitmap = Bitmap.createBitmap(webView.getWidth(),
Math.round(webView.getContentHeight() * webView.getScale()), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
webView.draw(canvas);
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, JPEG_COMPRESSION, out);
However, the bitmap is too big (more than 100 MB) and takes too much memory. Does anyone know any better method to keep the memory usage low?
The problem is the WebView is small, and the generated JPEG file is small but the intermediate Bitmap which is not used any more in other places is huge.
Set JPEG_COMPRESSION = 0-100 as it is a quality paramater of bitmap 0 meaning compress for small size, 100 meaning compress for max quality.
You can also set bitmap height and width using
BitmapFactory.Options bop
bop.outWidth
bop.outHeight
I am taking the picture and saving it in sdcard. And later some time I am saving the same picture to server by creating a file like the following.
ByteArrayOutputStream boas = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, boas);
resizeBitmap.compress(Bitmap.CompressFormat.JPEG ,100, boas);
byte[] b = boas.toByteArray();
The Byte array I am sending to the server.
But image quality and size is saving incorrect. If the resolution is 1200 X 1600 instead 320 * 240 is saving in server.
How are you saving your picture when it is taken? If you don't supply a URI you will only get a low resolution thumbnail in return.
EDIT: Code how to get full image from camera, I'm only guessing your problem right now;
Intent in = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File out = Environment.getExternalStorageDirectory();
String fileName="image.png";
out = new File(out, fileName);
in.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(in);
From the code you posted it only shows that you make a from a bitmap a PNG then a JPG with 0% dropping in quality.
I am creating an android app and want to read a file from sdcard and send it to a rest webservice. Everything works fine but when I read the file from sdcard its filesize is much bigger than its originalsize. A 10 KB file is getting 260 KB.
I am doing the following...
File f = new File(uri);
Log.d("ORIGINAL FILESIZE:"+f.length());
Filesize 10752 Bytes on SDCARD.
This is exactly the same size the image has on the phone.
Bitmap bmp = BitmapFactory.decodeFile(uri);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bao);
Log.d("FILESIZE AFTER:"+bao.size());
Filesize 260904 Bytes after decode / compress.
This is the filesize the server receives and writes to disk.
It's the same Image and Quality but about 20 times bigger.
Does anyone know what I am doing wrong?
If your original file is a jpeg with high compression rate, it's normal that the decoded bitmap is much larger, especially if you use a quality of 100 in the compress() method (which means high quality).
Did you try using compress() with a lower quality value (cf documentation) ?
It is not wrong. Or at least it could be. The bytes rapresentation of a bitmap in memory, requires (a 32-bit image) height * widht * 4 bytes. And of course it differs from the file size becaouse of the compression.