【发布时间】:2011-05-19 06:40:24
【问题描述】:
如何在丢失原始大小或质量的情况下将位图图像保存到 sdcard,我目前正在使用此代码,但它始终保存的图像比相机捕获的图像的原始大小小得多。
private void SaveImage(Bitmap bitmap, String strType) {
File sdImageMainDirectory = new File("/sdcard/");
if (sdImageMainDirectory.exists()) {
FileOutputStream fileOutputStream = null;
Date dt = new Date();
String nameFile = "myImage";
int quality = 100;
try {
String strFullImageName;
strFullImageName = UserID + "_" + nameFile + ".jpg";
fileOutputStream = new FileOutputStream(
sdImageMainDirectory.toString() + "/"
+ strFullImageName);
BufferedOutputStream bos = new BufferedOutputStream(
fileOutputStream);
bitmap.compress(CompressFormat.JPEG, quality, bos);
bos.flush();
bos.close();
UploadImage(bitmap, strFullImageName, strType);
} catch (FileNotFoundException e) {
Utilities.LogError(e);
} catch (IOException e) {
Utilities.LogError(e);
} catch (Exception eee) {
Utilities.LogError(eee);
}
} else {
Utilities.LogError("File not Found");
}
}
找到解决办法,
点击 KPBird 提供的链接后,我发现问题在于相机活动如何返回捕获的位图对象。
为了保护内存,相机 Activity 返回已捕获照片的缩放图像,因此解决方案是永远不要使用此缩放图像!
只需向相机 Intent 传递一个额外的内容,提供您要保存图像的 url,瞧!捕获的照片会以原始比例保存在您想要的位置
代码:
private static final int CAMERA_PIC_REQUEST = 1337;
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
imageNameToUpload = "myImage.jpeg";
cameraIntent.putExtra( android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File( imagePathToUpload+imageNameToUpload)));
startActivityForResult(cameraIntent,
CAMERA_PIC_REQUEST);
感谢 KPBird!
【问题讨论】:
标签: android