南山怀槿
南山怀槿

Reputation: 71

Delete a file in Android APP, but the file is still exist in MTP

I'm working on this these days...

My app delete somes files by itself. But when I connect my phone with PC by MTP mode, the files which have been delete are still 'alive', but cannot be copied or opened.

I know infact the files are already deleted, but I have to refresh MTP. I have tried the following method:

MediaScannerConnection.scanFile(getApplicationContext(), paths, null, null);

but it doesn't work...

Does anybody here knows how to fix this issue? Thanks!

Upvotes: 0

Views: 922

Answers (1)

dex
dex

Reputation: 5230

When you delete a file, Call refreshSystemMediaScanDataBase() method just after delete call trigger to refresh media

/** 
@param context : it is the reference where this method get called
@param docPath : absolute path of file for which broadcast will be send to refresh media database
**/
public static void refreshSystemMediaScanDataBase(Context context, String docPath){
   Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
   Uri contentUri = Uri.fromFile(new File(docPath));
   mediaScanIntent.setData(contentUri);
   context.sendBroadcast(mediaScanIntent);
}

Upvotes: 2

Related Questions