Android删除当前路径下的图片

  1. private void DeleteImage(String imgPath) {  
  2.     ContentResolver resolver = getActivity().getContentResolver();  
  3.     Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=?",  
  4.             new String[] { imgPath }, null);  
  5.     boolean result = false;  
  6.     if (cursor.moveToFirst()) {  
  7.         long id = cursor.getLong(0);  
  8.         Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;  
  9.         Uri uri = ContentUris.withAppendedId(contentUri, id);  
  10.         int count = getActivity().getContentResolver().delete(uri, nullnull);  
  11.         result = count == 1;  
  12.     } else {  
  13.         File file = new File(imgPath);  
  14.         result = file.delete();  
  15.     }  
  16.   
  17.     if (result) {  
  18.         imageList.remove(imgPath);  
  19.         adapter.notifyDataSetChanged();  
  20.         Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_LONG).show();  
  21.     }  
  22. }  

猜你喜欢

转载自blog.csdn.net/qq_35224776/article/details/80649741