I have an app developed on Android versions 4.0 and above. ( The app does not support Android versions below 4.0 [Ice Cream Sandwich] ).
The question is related to (print) DPI of various images ( for eg. of jpeg or png ) format.
This question does NOT relate to SCREEN DPI or sizes of various Android devices. It is also NOT related to showing the Bitmap on the device in screen size.
I am using the following code to load the image file in 'Bitmap'. Then I have been cropping it and saving it to another file in JPEG format with jpegCompression. I have been able to do this by the following code, but I am unable to get DPI of loaded or set the DPI of saved Image file.
So I have two questions.
1) How can I get the (print) DPI from the JPEG file, after or while loading it in 'Bitmap'?
2) While saving the new generated 'Bitmap', how can I set the DPI again in the JPEG file?
Following is the part of code for reference.
FileInputStream inputStream = new FileInputStream(theSourcePhotoFilePathName);
Bitmap bitmap = null;
BitmapRegionDecoder decoder = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
options.inDensity = 300; // Tried this but not working.
try {
decoder = BitmapRegionDecoder.newInstance(in, false);
bitmap = decoder.decodeRegion(region, options); // the region has cropping coordinates.
} catch (IllegalArgumentException e){
Log.d("First Activity", "Failed to recycle bitmap for rect=" + region, e);
} catch (IOException e) {
Log.d("First Activity", "Failed to decode into rect=" + region, e);
} finally {
if (decoder != null) decoder.recycle();
}
inputStream.close();
inputStream = null;
FileOutputStream fos = new FileOutputStream( theTargetTempFolderDestFilePath );
bitmap.compress(CompressFormat.JPEG, jpegCompressionRatio, fos);
fos.flush();
fos.close();
fos = null;
I have tried to find from stackoverflow and from other sites by googling, but could not get the proper related answer. So I have decided to ask it in this forum.
Your hints and suggestions are welcome.
Sanjay.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…