How to Refresh Mediastore on Android

How can I refresh MediaStore on Android?

Ok, I've done it.

Rather than rescan the card, the app iterates through all the playlists in mediastore and checks the length of the _data field. I discovered that for all the lists with no associated M3U file, this field was always empty. Then it was just a case of finding the source code for the original android music app, finding the delete method and using that to delete any playlists with a length of 0. I've renamed the app PlaylistPurge (since it doesn't 'rescan' anymore) and am posting the code below.

I'll probably also publish this somewhere, either on the Market or on my own site, http://roryok.com

package com.roryok.PlaylistPurge;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class PlaylistPurge extends ListActivity {

private List<String> list = new ArrayList<String>();
private final String [] STAR= {"*"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ListAdapter adapter = createAdapter();
setListAdapter(adapter);
}

/**
* Creates and returns a list adapter for the current list activity
* @return
*/
protected ListAdapter createAdapter()
{
// return play-lists
Uri playlist_uri= MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
Cursor cursor= managedQuery(playlist_uri, STAR, null,null,null);
cursor.moveToFirst();
for(int r= 0; r<cursor.getCount(); r++, cursor.moveToNext()){
int i = cursor.getInt(0);
int l = cursor.getString(1).length();
if(l>0){
// keep any playlists with a valid data field, and let me know
list.add("Keeping : " + cursor.getString(2) + " : id(" + i + ")");
}else{
// delete any play-lists with a data length of '0'
Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, i);
getContentResolver().delete(uri, null, null);
list.add("Deleted : " + cursor.getString(2) + " : id(" + i + ")");
}
}
cursor.close();
// publish list of retained / deleted playlists
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

return adapter;
}
}

UPDATE:

Here's a link to a post on my blog about the app http://roryok.com/blog/index.php/2010/07/23/clearing-out-deleted-playlists-in-android/

UPDATE 2: Tuesday, April 9, 2013

I've gotten a lot of traffic to my blog from this post, and a huge number of emails from people thanking me for it. Glad it helped out! Any potential users should know that my crappy app currently crashes as soon as you run it, but actually does what it's supposed to do! I've always intended to go back and fix this crashing behaviour and put it on the market, hopefully 2013 will be the year I do that.

Refresh MediaStore for Images & Videos

You should try this solution for amove and delete the image.

Pass file directory path inside deleteRecursive(fileOrDirectory) and you can delete multiple and the single image from file or directory.

public void deleteRecursive(File fileOrDirectory) {
if (!fileOrDirectory.isDirectory()) return;
File[] childFiles = fileOrDirectory.listFiles();
if (childFiles == null) return;
if (childFiles.length == 0) {
fileOrDirectory.delete();
} else {
for (File childFile : childFiles) {
deleteRecursive(childFile);
DeleteAndScanFile(MainActivity.this, childFile.getPath(), childFile);
}
}


}

private void DeleteAndScanFile(final Context context, String path,
final File fi) {
String fpath = path.substring(path.lastIndexOf("/") + 1);
try {
MediaScannerConnection.scanFile(context, new String[]{Environment
.getExternalStorageDirectory().toString()
+ "/abc/"
+ fpath.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
if (uri != null) {
context.getContentResolver().delete(uri, null,
null);
}
fi.delete();
}
});
} catch (Exception e) {
e.printStackTrace();
}

}

It's Good work for me, Hope this help you...if you need any help you can ask

How can refresh or restart MediaStore after adding or deleting or any modifying files on the SDcard?

I find response for my post.
i use this method after deleting or adding or any midifying on sdcard.

static void scanWithPath(Context cont,String[] pathes)
{
OnScanCompletedListener callback=new OnScanCompletedListener() {


public void onScanCompleted(String path, Uri uri) {
//the work which want to do when scan completed
}
};
MediaScannerConnection.scanFile(cont, paths, mimeTypes, callback);

}

we need only context and a string array from pathes which decide to scann those.
I write this method in a java calss.and becuse scanFile method need to a context so I passed it to method.
i wish this helped you.

Is it possible to refresh Media Store on Android Nougat?

Turns out I was trying to scan the path of the file I had copied to another place and then deleted, rather than the path of the newly created file.

With a combination of the media scanner and ACTION_MEDIA_SCANNER_SCAN_FILE and trying to scan the right file, I was able to refresh the media store.



Related Topics



Leave a reply



Submit