Android 从SD卡获取图像,压缩它,然后将其保存回SD?
我正在尝试从相机中捕获图像,如果图像太大,我想压缩位图并将其发送回sd卡。起初,我试图将图像直接拉入内存,但显然,根据我对上一个问题的回答,我可以做到:Android 从SD卡获取图像,压缩它,然后将其保存回SD?,android,android-camera,android-camera-intent,Android,Android Camera,Android Camera Intent,我正在尝试从相机中捕获图像,如果图像太大,我想压缩位图并将其发送回sd卡。起初,我试图将图像直接拉入内存,但显然,根据我对上一个问题的回答,我可以做到: 如何将该图像文件恢复到我的应用程序内存中?您从相机获取的图像已压缩为jpeg格式。您将永远无法从相机获得原始图像。您可以使用下面的代码重新缩放图像 Bitmap yourBitmap; Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true
如何将该图像文件恢复到我的应用程序内存中?您从相机获取的图像已压缩为jpeg格式。您将永远无法从相机获得原始图像。您可以使用下面的代码重新缩放图像
Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);
这会将图像压缩到更小的尺寸,您只需根据需要提供新的高度和宽度。单击按钮调用方法将图像保存到SD卡:
SaveImage(bitmap);
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();
}
}
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageview.setImageDrawable(bitmap);
将图像保存到SD卡:
SaveImage(bitmap);
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();
}
}
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageview.setImageDrawable(bitmap);
用于从SD卡获取图像:
SaveImage(bitmap);
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();
}
}
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageview.setImageDrawable(bitmap);
用于将图像存储到内存:
SaveImage(bitmap);
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();
}
}
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageview.setImageDrawable(bitmap);