我试图从相机捕捉图像,如果它太大,我想压缩位图并将其发送回SD卡。我最初试图将图像直接拉到内部记忆体,但显然根据这个答案我以前的问题,我可以做到这一点:android picture from camera being taken at a really small size从SD卡中取出图像,压缩并保存回SD?
我怎么能把该图像文件回到我的应用程序内部内存?
我试图从相机捕捉图像,如果它太大,我想压缩位图并将其发送回SD卡。我最初试图将图像直接拉到内部记忆体,但显然根据这个答案我以前的问题,我可以做到这一点:android picture from camera being taken at a really small size从SD卡中取出图像,压缩并保存回SD?
我怎么能把该图像文件回到我的应用程序内部内存?
您从相机获得的图像已经以jpeg格式压缩。你永远不会从相机获得原始图像。
您可以使用下面的代码重新调整图像大小。
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);
用于存储图像到内部存储器:
Saving and Reading Bitmaps/Images from Internal memory in Android