Question

I have developed android phonegap app.I am recording the audio through phonegap api and to move the file i have created a plugin to start media scanner.But media scanner is starting to run after 7 or 8 seconds.So i cannot able to do other process in the app.Is there is any solution to find media scanner is completed or not in android.

Here is my code to start media scanner:

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

Here is error in logcat:

 09-17 13:45:36.306: E/AndroidRuntime(706): FATAL EXCEPTION: main
 09-17 13:45:36.306: E/AndroidRuntime(706): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.example/com.app.example.MainActivity}: java.lang.NullPointerException
 09-17 13:45:36.306: E/AndroidRuntime(706):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at android.os.Handler.dispatchMessage(Handler.java:99)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at android.os.Looper.loop(Looper.java:123)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at android.app.ActivityThread.main(ActivityThread.java:4627)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at java.lang.reflect.Method.invokeNative(Native Method)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at java.lang.reflect.Method.invoke(Method.java:521)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at dalvik.system.NativeStart.main(Native Method)
 09-17 13:45:36.306: E/AndroidRuntime(706): Caused by: java.lang.NullPointerException
 09-17 13:45:36.306: E/AndroidRuntime(706):     at android.content.ContentResolver.registerContentObserver(ContentResolver.java:804)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at com.app.mobilyzer.MobilyzerActivity.isMediaScannerScanning(MobilyzerActivity.java:79)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at com.app.example.MainActivity.onCreate(MainActivity.java:72)
  09-17 13:45:36.306: E/AndroidRuntime(706):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 09-17 13:45:36.306: E/AndroidRuntime(706):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 09-17 13:45:36.306: E/AndroidRuntime(706):     ... 11 more

I need to show the progress bar until media scanner completed.Please kindly help me.Thanks In Advance.

Was it helpful?

Solution

Use below code to check if MediaScanner is running

 public static boolean isMediaScannerScanning(ContentResolver cr) {
        boolean result = false;
        Cursor cursor = query(cr, MediaStore.getMediaScannerUri(),
               new String [] {MediaStore.MEDIA_SCANNER_VOLUME},
                null, null, null);
        if (cursor != null) {
            if (cursor.getCount() == 1) {
                cursor.moveToFirst();
                result = "external".equals(cursor.getString(0));
            }
            cursor.close();
        }    
        return result;
    }

If you need a callback You could register for a content observer on MediaStore.getMediaScannerUri() . But when MediaScanner is going on you could receive too many changes on content observer.

getContentResolver().registerContentObserver( MediaStore.getMediaScannerUri(), false, contentobserver);

You will have to convert this to Phonegap code i guess.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top