[英]How to save a bitmap that I got from camera, to the sd card without losing it's original size and quality
如何将位图图像保存到SD卡而丢失原始尺寸或质量,我目前正在使用此代码,但它总是将图像保存得比相机捕获的图像的原始尺寸小得多。
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提供的链接后,我发现问题在于相机活动如何返回捕获的位图对象。
为了保存内存,相机活动返回捕获的照片的缩放图像,因此解决方案是永远不要使用此缩放图像!
只需传递一个额外的相机意图,提供您想要保存图像和voilaa!的网址,捕获的照片将保存在您想要的位置,并具有原始比例
码:
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!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.