我试图从相机捕捉图像,如果它太大,我想压缩位图并将其发送回 SD 卡。我最初试图将图像直接拉入内部存储器,但显然根据我之前问题的这个答案我可以做到这一点:来自相机的android图片正在以非常小的尺寸拍摄
我怎样才能将该图像文件带回我的应用程序内部存储器?
我试图从相机捕捉图像,如果它太大,我想压缩位图并将其发送回 SD 卡。我最初试图将图像直接拉入内部存储器,但显然根据我之前问题的这个答案我可以做到这一点:来自相机的android图片正在以非常小的尺寸拍摄
我怎样才能将该图像文件带回我的应用程序内部存储器?
您可以使用下面的代码重新缩放图像。
Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);
这会将您的图像压缩到更小的尺寸,您只需要根据您的要求提供新的高度和宽度。
Onclick 按钮调用方法将图像保存到 SD 卡:
SaveImage(bitmap);
将图像保存到 SD 卡:
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/demo_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
// Below code will give you full path in TextView
> txtPath1.setText(file.getAbsolutePath());
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
从 SD 卡获取图像:
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageview.setImageDrawable(bitmap);
将图像存储到内存:
您从相机获得的图像已经以 jpeg 格式压缩。您永远不会从相机中获得原始图像。