我正在尝试将位图保存到我的目录,但没有一点麻烦。
我的应用程序的作用是:
1.通过Intent打开内置摄像头应用程序。
public void openCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory()+ File.separator + "image.jpg");
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
2.然后将图片意图存储到'REQUEST_IMAGE_CAPTURE'并将图像保存到临时目录中。
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
3.然后将其转换为位图并从tmp目录加载到图像视图
protected void onActivityResult(int requestCode, int resultCode, Intent data){
//Check that request code matches ours:
if (requestCode == REQUEST_IMAGE_CAPTURE){
//Get our saved file into a bitmap object:
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
Bitmap image = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
imageView.setImageBitmap(image);
}
}
现在,我如何将imageview中的位图保存到图片目录中?
我已将权限输入我的清单
<uses-feature android:name="android.hardware.camera"
android:required="true" />
...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
答案 0 :(得分:0)
使用bitmap.compress将位图指向OutputStream。
例如:
FileOutputStream fo = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fo); // bmp is your Bitmap
答案 1 :(得分:0)
private void saveImage(Bitmap bmp, String filePath){
File file = new File(filePath);
FileOutputStream fo = null;
try {
boolean result = file.createNewFile(); // true if created, false if exists or failed
fo = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY_PERCENT, fo);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fo != null) {
try {
fo.flush();
fo.close();
} catch (Exception ignored) {
}
}
}
}
请注意,filePath是目录+文件名,例如/sdcard/iamges/1.jpg