스터디/Android+Java

Android Force MediaScan (2012.04.26)

Dalmangyi 2017. 10. 7.

Util 클래스를 만들어서 아래와 같은식으로 Context와 BroadcastReceiver를 던져서 static변수로 만들었다.

private static final String MEDIA_MOUNTED = Intent.ACTION_MEDIA_MOUNTED;

public static final String SCAN_START = Intent.ACTION_MEDIA_SCANNER_STARTED;

public static final String SCAN_FINISH = Intent.ACTION_MEDIA_SCANNER_FINISHED;

public static void RefreshDB(Context mContext, BroadcastReceiver mReceiver, String folderPath) {

IntentFilter intentFilter = new IntentFilter(SCAN_START);

intentFilter.addAction(SCAN_FINISH);

intentFilter.addDataScheme("file");

mContext.registerReceiver(mReceiver, intentFilter);

        

mContext.sendBroadcast(new Intent(MEDIA_MOUNTED, Uri.parse("file://"+Environment.getExternalStorageDirectory())));

}



sendBroadcast의 uri주소를 정확한 파일명을 입력하면 그 파일에 대해서는 Media DB에 추가하는 것은 가능하다.

하지만 특정 폴더만 골라서 DB에 추가하는 방법은 모르겠다.


현재는 Environment.getExternalStorageDirectory()라는 함수를 통해서 기기에 따른 sdcard 주소만을 내 뱉어주어.

그 주소를 참조하여 미디어 스캔하고 있다.




브로드캐스트리시버는 아래와 같이 만들면 된다.




private BroadcastReceiver mReceiverMediaDB = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if(action.equals(MediaDBManager.SCAN_START)){

 

}

else if(action.equals(MediaDBManager.SCAN_FINISH)){


          }

     }

};

댓글