这开始作为Android论坛上的一般用户问题。然而,它必然是一个编程问题。这是我的问题。如何在Android上刷新MediaStore?
Android有一个服务 - MediaScanner - 它随时在后台运行(我相信)SD卡是未安装和重新安装的。该服务收集卡上所有媒体文件的数据,并提供可由音乐应用程序查询的SQLite数据库。大多数音乐应用程序使用此服务,因为它可以节省与扫描SD卡相关的电池耗尽。
自从我开始使用android之后,我一直有一个问题,M3U播放列表与设备同步,即使在从SD卡中删除后,仍保留在此SQLite数据库中。现在,我已经收集了大约40个播放列表,并显示在我使用的任何音乐应用程序中,尽管卡上只有大约10 m3u的文件。剩余的播放列表不播放,并且是空的。我可以通过从音乐应用中删除它们手动删除它们,但我厌倦了这样做。必须有更好的方法来删除这些幽灵播放列表。
Android Market上有两个应用程序 - SDRescan和音乐扫描仪,据说可以做到这一点,但它们都不起作用。
我开始着手编写自己的应用程序来刷新或删除MediaStore数据库并从头开始,但我并没有走得太远。我有它运行下面的代码的Android应用:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
我发现这段代码的几个例子在线,以此来扫描SD卡,但我没有与任何任何运气。有小费吗?
全码:
package com.roryok.MediaRescan;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
public class MediaRescan extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
setContentView(R.layout.main);
}
//Rescan the sdcard after copy the file
private void rescanSdcard() throws Exception{
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory()));
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter.addDataScheme("file");
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
}
嗨,你知道,如果sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED, Uri.parse( “文件://” + Environment.getExternalStorageDirectory())));将工作的SD卡是额外的插槽卡?换句话说getExternalStorageDirectory()返回一个sdcard的路径,这是内部的,但可能有制造商提供的外部卡?如何调用这种卡的媒体扫描仪? – Ahmed 2013-01-26 20:54:15
目前似乎没有办法做到这一点。看到这个线程:http://stackoverflow.com/questions/5453708/android-how-to-use-environment-getexternalstoragedirectory – roryok 2013-01-28 10:30:44