通过在我的应用程序中使用相机意图拍照,图像从 SD 卡和图库中复制。这是我的拍照代码:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = "file to /storage/emulated/0/Pictures/folder/image.jpg"
} catch (IOException ex) {
// Error occurred while creating the File
Log.e("Can't create file", ex.getLocalizedMessage());
}
Uri photoUri = Uri.fromFile(photoFile);
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
}
图像保存到
photoFile
,另一个相同的图像保存到图库。怎么解决?
我假设您希望将图像保存在 SD 卡中而不是图库中?如果是这样,请尝试将照片保存在设备外部存储器中应用程序的私人空间中,而不是保存在公共图片目录中。我在我的应用程序中这样做了:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File photoFile = File.createTempFile(imageFileName, ".jpg", storageDir);
我使用时间戳将图像保存在本地文件中并避免图像重复
ContentValues contentValues = new ContentValues();
ContentResolver resolver = getContentResolver();
String timeStamp = new SimpleDateFormat( "yyyyMMdd_HHmmss", Locale.US).format( new Date() );
contentValues.put( MediaStore.MediaColumns.DISPLAY_NAME, "Image_" + timeStamp + ".jpg" );
contentValues.put( MediaStore.MediaColumns.MIME_TYPE, "image/jpeg" );
contentValues.put( MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + "ENTER YOUR FILE NAME" );
imageUri = resolver.insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues );