Android:将图像保存到目录 [英] Android: Saving an image to directory
问题描述
我正在尝试将位图保存到我的目录,但没有一点麻烦。
I'm trying to save a bitmap to my directory but haven't a little trouble.
我的应用程序的作用是:
What my applications does is:
1.通过Intent打开内置摄像头应用程序。
1.Opens the inbuilt camera application by an 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'并保存图像进入临时目录。
2.It then stores the picture intent into 'REQUEST_IMAGE_CAPTURE' and saves the image into a temp directory.
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
3.然后将其转换为位图并从tmp目录加载到图像视图中
3.Which is then converted into a bitmap and loaded from the tmp directory into an image view
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进入图片目录?
Now, how do i save the bitmap which in the imageview into the picture directory?
我已将权限输入我的清单
I have inputted the permissions into my manifest
<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"/>
推荐答案
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
Note that filePath is directory + filename e.g. /sdcard/iamges/1.jpg
这篇关于Android:将图像保存到目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!