Java Android在查看后保存图像
我使用以下代码打开相机,拍摄图像并在imageview中显示 我假设在onActivityResult上我应该执行bm.save type命令,但在那里我看不到。理想情况下,我希望将其保存为SD卡上的jpeg格式。 任何帮助都将不胜感激 汤姆Java Android在查看后保存图像,java,android,android-camera,Java,Android,Android Camera,我使用以下代码打开相机,拍摄图像并在imageview中显示 我假设在onActivityResult上我应该执行bm.save type命令,但在那里我看不到。理想情况下,我希望将其保存为SD卡上的jpeg格式。 任何帮助都将不胜感激 汤姆 在onActivityResult中,可以像这样存储位图 File root = new File(Environment.getExternalStorageDirectory(), "Directory"); if (!roo
在onActivityResult中,可以像这样存储位图
File root = new File(Environment.getExternalStorageDirectory(), "Directory");
if (!root.exists()) {
root.mkdirs();
}
String fname = "Image.png";
File file = new File (root, fname);
try {
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush(); out.close();
} catch (Exception e) {
e.printStackTrace();
}
这应该能奏效。使用位图(相机附带的)和您选择的文件名调用以下方法,例如“myEpicPic” (我想您知道何时需要保存位图) 如果要再次从内存中读取位图:
public Bitmap readBitmapFromMemory(String filename) {
Bitmap defautBitmap = null;
File filePath = this.getFileStreamPath(filename);
FileInputStream fi;
try {
fi = new FileInputStream(filePath);
defautBitmap = BitmapFactory.decodeStream(fi);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
return defautBitmap;
}
我希望这能有所帮助。非常感谢你的帮助,伙计。有一点似乎被夸大了。我正试图保存到SD卡,但出现了一个错误:()我添加了例程,并添加了WriteBitMatoMemory(“/mnt/extSdCard/objectattributesID/image.png”,bm);为了我的积极性结果
public void writeBitmapToMemory(String filename, Bitmap bitmap) {
FileOutputStream fos;
// Use the compress method on the Bitmap object to write image to the OutputStream
try {
fos = this.openFileOutput(filename, Context.MODE_PRIVATE);
// Writing the bitmap to the output stream
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public Bitmap readBitmapFromMemory(String filename) {
Bitmap defautBitmap = null;
File filePath = this.getFileStreamPath(filename);
FileInputStream fi;
try {
fi = new FileInputStream(filePath);
defautBitmap = BitmapFactory.decodeStream(fi);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
return defautBitmap;
}