我想通过提供路径从图库中删除图像。我已经尝试了以下的解决方案,但没有一个能够解决问题。在第一个我得到文件被删除的日志,但我仍然可以在库中看到它。在第二个中,我得到Log.d("TAG", "File not found");
#1
private void deleteImage() {
photoOne = mHome.getPhotoOne();
File fdelete = new File(photoOne);
if (fdelete.exists()) {
if (fdelete.delete()) {
Log.e("-->", "file Deleted :" + photoOne);
callBroadCast();
} else {
Log.e("-->", "file not Deleted :" + photoOne);
}
}
}
public void callBroadCast() {
Log.e("-->", " >= 14");
MediaScannerConnection.scanFile(getContext(), new String[]{Environment.getExternalStorageDirectory().toString()}, null, new MediaScannerConnection.OnScanCompletedListener() {
/*
* (non-Javadoc)
* @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
*/
public void onScanCompleted(String path, Uri uri) {
Log.e("ExternalStorage", "Scanned " + path + ":");
Log.e("ExternalStorage", "-> uri=" + uri);
}
});
}
#2
private void deletePhoto() {
photoOne = mHome.getPhotoOne();
File file = new File(photoOne);
// Set up the projection (we only need the ID)
String[] projection = {MediaStore.Images.Media._ID};
// Match on the file path
String selection = MediaStore.Images.Media.DATA + " = ?";
String[] selectionArgs = new String[]{file.getAbsolutePath()};
// Query for the ID of the media matching the file path
Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = getContext().getContentResolver();
Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
if (c.moveToFirst()) {
// We found the ID. Deleting the item via the content provider will also remove the file
long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
contentResolver.delete(deleteUri, null, null);
} else {
// File not found in media store DB
Log.d("TAG", "File not found");
}
c.close();
}
这是我从相机获取图片的方式
private void takePhoto() {
Intent takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try {
photoFile = photoPath();
} catch (IOException e) {
e.printStackTrace();
}
Uri photoURI = FileProvider.getUriForFile(getContext(),
getContext().getApplicationContext().getPackageName() + ".provider",
photoFile);
takePhoto.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
if (whichImage == 1) {
startActivityForResult(takePhoto, REQUEST_CAMERA_FIRST_CHALLENGE);
} else if (whichImage == 2) {
startActivityForResult(takePhoto, REQUEST_CAMERA_SECOND_CHALLENGE);
}
}
private File photoPath() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String photoFileName = "PHOTO_" + timeStamp + "_";
File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File photoFile = new File(storageDirectory, photoFileName);
if (whichImage == 1) {
mPhotoLocation = photoFile.getAbsolutePath();
} else if (whichImage == 2) {
mPhotoLocation2 = photoFile.getAbsolutePath();
}
return photoFile;
}
如果是从图库
中选择的,我就是这样拍摄的private void selectImage() {
Intent selectPhoto = new Intent(Intent.ACTION_PICK);
selectPhoto.setType("image/*");
if (whichImage == 1) {
startActivityForResult(Intent.createChooser(selectPhoto, "Select image"), REQUEST_GALLERY_FIRST_CHALLENGE);
} else if (whichImage == 2) {
startActivityForResult(Intent.createChooser(selectPhoto, "Select image"), REQUEST_GALLERY_SECOND_CHALLENGE);
}
}
private String galleryPath(Uri uri) {
Cursor cursor = null;
try {
String[] projection = {MediaStore.Images.Media.DATA};
cursor = getContext().getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}