[英]android - save an image into gallery
I am attempting to save an image into the android gallery, but unfortunately, it is not showing up. 我正在尝试将图像保存到android画廊中,但是不幸的是,它没有出现。
My picture callback is as follows: 我的图片回调如下:
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFileDir = getDir("images", 0);
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
Toast.makeText(getApplicationContext(), "Can't create directory to save image.",
Toast.LENGTH_LONG).show();
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
File pictureFile = new File(filename);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
Toast.makeText(getApplicationContext(), "New Image saved:" + photoFile,
Toast.LENGTH_LONG).show();
//Insert image into gallery
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + pictureFile.getPath());
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, photoFile , "My Image");
} catch (Exception error) {
Toast.makeText(getApplicationContext(), "Image could not be saved.",
Toast.LENGTH_LONG).show();
}
//...
}
};
I believe that the image is saved properly in the app data, but it does not show up in the gallery. 我认为该图像已正确保存在应用程序数据中,但未显示在图库中。
My app requires these two permissions: 我的应用程序需要以下两个权限:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Unfortunately, this code still does not work. 不幸的是,此代码仍然无法正常工作。 Is there anything incorrect?
有什么不对吗?
Simply an incorrect filepath. 只是错误的文件路径。
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + pictureFile.getPath());
needs to me 需要我
Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getPath());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.