我用這個功能來保存位圖上的SD卡文件:如何在Android中將位圖保存到位圖時設置dpi圖像?
private static File storeImage(Context context, Bitmap image) {
File pictureFile = getOutputMediaFile(context);
if (pictureFile == null) {
return null;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return pictureFile;
}
private static File getOutputMediaFile(Context context){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ context.getPackageName()
+ "/Files");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
File mediaFile;
String mImageName="IMG_"+ timeStamp +".png";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}
當我打開這個文件,看看圖像DPI信息,它表明72個像素/英寸 like this
我如何可以設置它到300像素/英寸,或其他值?
一種方法是更改PNG元數據。這很複雜,搜索谷歌和亂七八糟的字節。 –
相關:https://stackoverflow.com/questions/27323561/do-pngs-or-jpgs-have-a-dpi-or-is-it-irrelevant-when-building-for-retina –