如何将相机拍摄的图像存储在图库中?
How to store image captured from camera in gallery?
我想存储从图库中的图像中捕获的图像。我不希望它们存储在 SD 卡上。如何实现。我有以下代码
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, tempuri);
startActivityForResult(i, CHOOSE_CAMERA_RESULT);
tempuri
应该是什么?
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为保存图片的文件夹名称。您可以根据需要更改这些。
我想存储从图库中的图像中捕获的图像。我不希望它们存储在 SD 卡上。如何实现。我有以下代码
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, tempuri);
startActivityForResult(i, CHOOSE_CAMERA_RESULT);
tempuri
应该是什么?
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为保存图片的文件夹名称。您可以根据需要更改这些。