Question

I'm relatively new to Android and can't get this working.

So far I have an app which has one large circular red button. When this is pressed by the user it executes an operation (at the moment it displays 'Hello World').

Instead, I would like it to delete all photos on the Android device. (Yes, it is a strange thing to do, I know)

My questions:

  1. Is this possible? I've had trouble getting it to work and can't find apps which have done this before.

  2. The only Android device I have is a Nexus 7 tablet. I would like it to work on both Android phones and tablets. Is this also possible, as I understand that their directory structures vary?

Was it helpful?

Solution

Yes you can delete Images but I do not suspect you can do them all at once.

I recommend you query the Mediastore to get the _ID of all Images and then loop through the list and delete them. Here is some sample code but i'm sure it can be better optimized. This code is filesystem agnostic so it should work anywhere.

Code to get list of all items on Mediastore.Images

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

    Cursor c = context.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);
            c.moveToNext();
        }
        c.close();
    }

Then to delete, well you could do that right in the original loop or loop through the arraylist, but here is how to delete:

context.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, BaseColumns._ID + "=?", new String[]{Long.toString(mediaStoreIds.get(i))});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top