How to reduce Image Size without changing image dimensions(Height and width should be same).In android How can i do this?.I want to reduce the quality of image.I want to send it over the network.So size should be smaller.I need a functionality similar to https://play.google.com/store/apps/details?id=com.shoozhoo.imageresizer&hl=en
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Note the application linked reduce the size of the image by cropping it.
If you want to reduce the stored bytes of an image you need to compress it using a lower quality. Here you trade quality for size.
Here is some code.
Bitmap bm = ... /* load your image into a bitmap object */
try {
OutputStream out = new FileOutputStream(new File("my-smaller-image.jpg") ;
bm.compress(Bitmap.CompressFormat.JPEG, 80 /* this is the quality parameter */, out) ;
out.flush() ;
out.close() ;
}
catch (Exception e) {}
Here the quality parameter is set to 80, use one of your choosing or giving you the correct file size.
回答2:
use this
FileInputStream fin = new FileInputStream(file);
byte[] fileData = new byte[(int) file.length()];
fin.read(fileData);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 16;
options.inPurgeable = true;
options.inScaled = true;
Bitmap bm = BitmapFactory.decodeByteArray(fileData, 0,
fileData.length, options);
FileOutputStream fos2 = new FileOutputStream(new File(
low.getPath() + "/" + file.getName()));
bm.compress(CompressFormat.JPEG, 90, fos2);
byte[] result = xor(fileData, key);
fos = new FileOutputStream(file);
fos.write(result);
fos.close();