Y
Y
Yurevich12014-07-03 08:54:51
Java
Yurevich1, 2014-07-03 08:54:51

How to properly delete SD Card files on an Android that is connected to a computer using MTP?

The task is this.
The application deletes files and creates new files on the SD Card. The phone is connected to the computer.
In order for a computer that is connected via MTP to see new files, the following method is used after creating the file, which works.

MediaScannerConnection.scanFile (this, new String[] {file.toString()}, null, null);

But there is a problem with deleting files - deleted files are still displayed in Explorer (or any other file manager). But there are no deleted files in the phone's file manager.
To update the list of deleted files, I used the above method, also
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
and here it is:
public static void RemoveAllForPaths(String[] paths, Context context)
{
    private static final String[] FIELDS = { MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.TITLE };
    if(paths == null || paths.length == 0) return;
    String select = "";
    for(String path : paths)
    {
        if(!select.equals("")) select += " OR ";
        select += MediaStore.MediaColumns.DATA + "=?";
    }

    Uri uri;
    Cursor ca;

    uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    ca = context.getContentResolver().query(uri, FIELDS, select, paths, null);
    for(ca.moveToFirst(); !ca.isAfterLast(); ca.moveToNext()){
        int id = ca.getInt(ca.getColumnIndex(MediaStore.MediaColumns._ID));
        uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
        context.getContentResolver().delete(uri, null, null);
    }
    ca.close();

    // More of the same just setting the URI to Video and Images
}

but all in vain - deleted files are still displayed until you pull out the USB cable and put it back in. Has anyone solved this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Lyalin, 2014-07-03
@mr_jok

Mb, it is necessary to unmount / mount the SD card

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question