【问题标题】:How to store image captured from camera in gallery?如何将相机拍摄的图像存储在图库中?
【发布时间】:2015-01-13 10:04:25
【问题描述】:
我想存储从图库中的图像捕获的图像。我不希望它们存储在 sdcard 上。如何实现。我有以下代码
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, tempuri);
startActivityForResult(i, CHOOSE_CAMERA_RESULT);
tempuri 应该是什么?
【问题讨论】:
标签:
java
android
android-camera
gallery
【解决方案1】:
tempuri=Uri.fromFile(getOutputFromCamera());
getOutputFromCamera() 在哪里
private File getOutputFromCamera() {
File storageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
AppConstants.FOLDER_NAME);
if (!storageDir.exists()) {
if (!storageDir.mkdirs()) {
Log.i(TAG, "Failed to create directory " + storageDir
+ AppConstants.FOLDER_NAME);
Toast.makeText(this, "Failed to create Directory",
Toast.LENGTH_SHORT).show();
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File imageFile = new File(storageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".png");
return imageFile;
}
其中 Environment.DIRECTORY_PICTURES 是默认图片目录,AppConstants.FOLDER_NAME 是保存图片的文件夹名称。您可以根据需要更改这些设置。