如何在不压缩的情况下将相机输出的图像直接保存到文件中?
How can I save output image from camera directly to file without compression?
文件压缩后发短信,再次压缩,非常卡顿
我用 Intent 调出相机。我得到结果并调出 Intent to send as text。
private void takePic() {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 2);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
File file = writebitmaptofilefirst("route_image",photo);
Uri uri = Uri.fromFile(file);
fileToDelete = file;
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra("address", "8001111222");
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("image/jpg");
startActivityForResult(sendIntent,3);
}
else if (requestCode == 3){
if (fileToDelete.exists()) fileToDelete.delete();
}
}
public static File writebitmaptofilefirst(String filename, Bitmap source) {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extStorageDirectory + "/temp_images");
if (!mFolder.exists()) {
mFolder.mkdir();
}
OutputStream outStream = null;
File file = new File(mFolder.getAbsolutePath(), filename + ".jpg");
if (file.exists()) {
file.delete();
file = new File(extStorageDirectory, filename + ".jpg");
Log.e("file exist", "" + file + ",Bitmap= " + filename);
}
try {
outStream = new FileOutputStream(file);
source.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("file", "" + file);
return file;
}
这很好用,除了生成的文本图像不连贯。如果我发送最终出现在画廊中的相同图片,它会将其转换下来,结果在接收端会好 100 倍。我可以直接保存从相机调用中获取的位图而不压缩,发送图片后文件将被删除。用户实际拍摄照片,然后点击默认短信应用程序上的发送按钮。
而不是
source.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
使用
source.compress(Bitmap.CompressFormat.PNG, 100, outStream);
JPEG 是一种有损格式,因此结果不稳定。
不压缩直接保存图片使用AndroidBmpUtil:
new AndroidBmpUtil().save(source, file);
文件压缩后发短信,再次压缩,非常卡顿
我用 Intent 调出相机。我得到结果并调出 Intent to send as text。
private void takePic() {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 2);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
File file = writebitmaptofilefirst("route_image",photo);
Uri uri = Uri.fromFile(file);
fileToDelete = file;
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra("address", "8001111222");
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("image/jpg");
startActivityForResult(sendIntent,3);
}
else if (requestCode == 3){
if (fileToDelete.exists()) fileToDelete.delete();
}
}
public static File writebitmaptofilefirst(String filename, Bitmap source) {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extStorageDirectory + "/temp_images");
if (!mFolder.exists()) {
mFolder.mkdir();
}
OutputStream outStream = null;
File file = new File(mFolder.getAbsolutePath(), filename + ".jpg");
if (file.exists()) {
file.delete();
file = new File(extStorageDirectory, filename + ".jpg");
Log.e("file exist", "" + file + ",Bitmap= " + filename);
}
try {
outStream = new FileOutputStream(file);
source.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("file", "" + file);
return file;
}
这很好用,除了生成的文本图像不连贯。如果我发送最终出现在画廊中的相同图片,它会将其转换下来,结果在接收端会好 100 倍。我可以直接保存从相机调用中获取的位图而不压缩,发送图片后文件将被删除。用户实际拍摄照片,然后点击默认短信应用程序上的发送按钮。
而不是
source.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
使用
source.compress(Bitmap.CompressFormat.PNG, 100, outStream);
JPEG 是一种有损格式,因此结果不稳定。
不压缩直接保存图片使用AndroidBmpUtil:
new AndroidBmpUtil().save(source, file);