在开发自定义图片查看中有一个功能是删除本地图片。同时图片查看中有一个功能是收藏图片,收藏的图片存入了自己写的一个图片收藏的表里了。页面如下:
要删除图片,要分两种情况,一种是该图片是收藏的图片,一种是不是。如果不是收藏的图片,则只需直接删除文件,同时通知系统更新本地图片媒体库。如果是收藏的图片,除了上述操作,还需要删除在收藏图片的表中对应的图片。
private void deleteImage(String imgPath) { String imgRealPath = getRealPathFromUri(activity,Uri.parse(imgPath)); Log.e(TAG, "deleteImage,imgRealPath:"+imgRealPath+",imgPath:"+imgPath);//imgRealPath:/storage/emulated/0/faceallRecog/IMG_20171024_140811.jpg,
ContentResolver resolver = activity.getContentResolver(); Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=?", new String[] { imgRealPath }, null); boolean result = false; if (cursor.moveToFirst()) { long id = cursor.getLong(0); Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; Uri uri = ContentUris.withAppendedId(contentUri, id); int count = activity.getContentResolver().delete(uri, null, null); result = count == 1; } else { File file = new File(imgRealPath); result = file.delete(); } //如果要删除的图片是收藏的图片,则要从收藏的表里删除 if(favFlag) { helper.deleteFavouritesImg(imgId); } if (result) { currentLabelList.remove(imgRealPath); myAdapter.notifyDataSetChanged(); showToast(activity,"删除成功"); } }imgPath:content://media/external/images/media/7556
/** * 根据图片的uri获取图片的绝对路径 * @param context * @param uri * @return */ public static String getRealPathFromUri(Context context, Uri uri){ if(context == null || uri == null) { return null; } if("file".equalsIgnoreCase(uri.getScheme())) { return getRealPathFromUri_Byfile(context,uri); } else if("content".equalsIgnoreCase(uri.getScheme())) { return getRealPathFromUriApi11to18(context,uri); } return getRealPathFromUri_AboveApi19(context,uri); } /** * 适配api19以上,根据uri获取图片的绝对路径 * @param context * @param uri * @return */ @SuppressLint("NewApi") private static String getRealPathFromUri_AboveApi19(Context context, Uri uri) { String filePath = null; String wholeId = null; wholeId = DocumentsContract.getDocumentId(uri); //使用“:”分割 String id = wholeId.split(":")[1]; String[] projection = {MediaStore.Images.Media.DATA}; String selection = MediaStore.Images.Media._ID + "=?"; String[] selectionArgs = {id}; Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null); int columnIndex = cursor.getColumnIndex(projection[0]); if(cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } /** * 适配api11-api18,根据uri获取图片的绝对路径。 * 针对图片URI格式为Uri:: content://media/external/images/media/1028 * @param context * @param uri * @return */ private static String getRealPathFromUriApi11to18(Context context, Uri uri) { String filePath = null; String[] projection = {MediaStore.Images.Media.DATA}; CursorLoader cursorLoader = new CursorLoader(context,uri,projection,null,null,null); Cursor cursor = cursorLoader.loadInBackground(); if(cursor != null) { cursor.moveToFirst(); filePath = cursor.getString(cursor.getColumnIndex(projection[0])); cursor.close(); } return filePath; } //针对图片URI格式为Uri:: file:///storage/emulated/0/DCIM/Camera/IMG_20170613_132837.jpg private static String getRealPathFromUri_Byfile(Context context, Uri uri) { String uri2Str = uri.toString(); String filePath = uri2Str.substring(uri2Str.indexOf(":") + 3); return filePath; }
现在的问题是不能实时刷新,只能重新进入页面才能看到删除的结果。这一点需要继续改进。//图片收藏取消 public void deleteFavouritesImg(int imgId){ db = mDatabaseHelper.getWritableDatabase(); db.execSQL("delete from "+TAB_FAVOURITES + " where "+FAV_IMG_ID+" = ? ",new Object[]{imgId}); }
http://blog.csdn.net/dingxu_ren/article/details/38823591
http://blog.csdn.net/mingyunxiaohai/article/details/72772126
http://blog.csdn.net/yanzi1225627/article/details/29252665
http://www.jianshu.com/p/c283fd5413d5