简体   繁体   中英

Android how to remove image from gallery

I have get data all images in gallery from my phone. But when i want action delete one image selected and can't delete image. How to use java or kotlin to delete image in gallery?. Sorry my english very bad. My code to get all image from gallery:

```   val imageProjection = arrayOf(
            MediaStore.Images.Media._ID,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Images.Media.DATE_TAKEN,
            MediaStore.Images.Media.SIZE,
            MediaStore.Images.Media.DATA,
            MediaStore.Images.Media.DISPLAY_NAME,
        )

        val imageSortOrder = "${MediaStore.Images.Media.DATE_ADDED} DESC"

        val cursor = requireActivity().contentResolver.query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            imageProjection,
            null,
            null,
            imageSortOrder
        )
        cursor.use {
            if (cursor != null) {
                var display_name1=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME)
                val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
                val data=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
                val date_taken=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_TAKEN)
                val id=cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
                val name=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME)
                val size=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE)
                val display_name=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME)
                while (cursor.moveToNext()) {
                    allImages.add(ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getLong(idColumn)))
                    var path=cursor.getString(data)
                    var file:File=File(path)
                    var sizeLong=cursor.getLong(size)
                    var dateTaken=cursor.getLong(date_taken)
                    listArrayImages.add(
                        ImageData(
                            ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getLong(idColumn)),
                            path,
                            file.name,
                            sizeLong,
                            getDate(dateTaken,"dd/MM/yyyy hh:mm:ss")
                        )
                    )
                }
            }
            else {

            }
        } ```

And code to delete image:

``` var file: File:File(image_path.toString)
delete(requireActivity(),file)
 fun delete(context: Context, file: File): Boolean {
        val where = MediaStore.MediaColumns.DATA + "=?"
        val selectionArgs = arrayOf(
            file.absolutePath
        )
        val contentResolver = context.`enter code here`contentResolver
        val filesUri = MediaStore.Files.getContentUri("external")
        contentResolver.delete(filesUri, where, selectionArgs)
        if (file.exists()) {
            contentResolver.delete(filesUri, where, selectionArgs)
        }
        return !file.exists()
    } ```

But delete function return false and still can't delete image from gallery

I have not much idea about your error but Try this Simple solution to delete image of gallery. Hope it may be Helpful to you.

private void deleteImageFromGallery() {
        
        String myGalleryImageFilePath;

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            myGalleryImageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + Environment.DIRECTORY_PICTURES + File.separator + getString(R.string.app_folder_name) + "/" + imageName;
        } else {
            myGalleryImageFilePath = Environment.getExternalStorageDirectory() + File.separator + getString(R.string.app_folder_name) + "/" + imageName;
        }

        File galleryImageDelete = new File(myGalleryImageFilePath);

        if (galleryImageDelete.exists()){
            if (galleryImageDelete.delete()) {
                Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Not Deleted", Toast.LENGTH_SHORT).show();
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM