我目前正在尝试将从手机拍摄的图像保存到它的图库中,但只有在弹出选择器对话框时我选择股票相机应用程序时,下面的代码才有效。每当我选择另一个相机应用程序(例如 Google 相机)时,拍摄的照片不会保存在任何地方。
更奇怪的是,有时图片确实会出现在图库的指定目录中,但 15 分钟左右后,当我使用股票相机应用程序时也是如此:图片将保存在默认目录中camera shots 目录,但要显示在其指定目录中需要相当长的时间,如果它完全显示在那里的话。
// Capturing Camera Image will launch camera app request image capture
void captureImage() {
//file uri to store image.
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
// Request camera app to capture image
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
getActivity().startActivityForResult(captureIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
最佳答案
嗯,
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
不再工作了。
你应该这样做:
调用相机 Activity :
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
和 onActivityResult :
if (data.getData() == null) {
Bitmap bm = (Bitmap)
data.getExtras().get("data");
String timeStamp = new SimpleDateFormat(
"yyyyMMdd_HHmmss").format(new Date());
File pictureFile = new File(Environment
.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
.getAbsolutePath()
+ File.separator + "IMG_" + timeStamp);
try {
FileOutputStream fos = new FileOutputStream(
pictureFile);
bm.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
String filePath = pictureFile.getAbsolutePath();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } else {
Uri imgUri =data.getData());
关于android - 非库存相机的相机应用拍摄照片时未保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25234897/