Search code examples
androidfile-get-contentsdelete-file

Delete all photos from Android device


In my app there is a button that will delete all photos in the phone before its been sold, so no one can see photos in the phone.

this code behind this button is this

      List<Long> mediaStoreIds = new ArrayList<Long>();

        Cursor c = getApplicationContext().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{BaseColumns._ID}, null, null, null);

        if (c != null) {
            final int id = c.getColumnIndexOrThrow(BaseColumns._ID);

            c.moveToFirst();
            while (!c.isAfterLast()) {
                Long mediaStoreId = c.getLong(id);

                mediaStoreIds.add(mediaStoreId);
                getApplicationContext().getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, BaseColumns._ID + "=?", new String[]{Long.toString(mediaStoreId)});

                c.moveToNext();
            }
            c.close();
        }


    }

the problem is that when debugger reach this line it stops

Cursor c = getApplicationContext().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{BaseColumns._ID}, null, null, null);

not sure why it stops and how to fix it

anyone can help me here?

Thanks


Solution

  • Not necessarily answering the question, but a couple bits of advice:

    • Deleting photos from the device's MediaStore does not remove all traces of them. Specifically, many devices maintain a thumbnail cache that contains thumbnails of all photos, even deleted ones. These thumbnails often have enough resolution to see faces and text. The thumbnail cache needs to be deleted separately, and its location varies with different device models.

    • The proper way to wipe data from an Android device is to perform a factory-reset, which is a function that all devices offer. And even then, the factory reset on some devices doesn't perform a full wipe of the internal memory, which means that the photos might still be recoverable if the device is rooted.