Android Question Clean media library after delete image, song, etc. using JavaObject library

dagofo

Member
Licensed User
Longtime User
After reading this post about JavaObject library http://www.b4x.com/android/forum/threads/javaobject-library.34486/

I try to write code to clean media library when one file is deleted. There is a partial solution described in the thread comented before but I don't know how translate to B4A one line.

I found the soluction described in Java based on "onScanCompleted" event (see code below)

Any idea??

Thanks in advance

B4X:
Private void scanaddedFile(String Path) {
    Try {
        MediaScannerConnection.scanFile(context, new String[] { Path },
                Null, new MediaScannerConnection.OnScanCompletedListener() {
                    Public void onScanCompleted(String Path, Uri Uri) {
                        Log.i("ExternalStorage", "Scanned " + Path + ":");
                        Log.i("ExternalStorage", "-> uri=" + Uri);

///////////// This line clean the media library in ScanCompleted event /////////////////////
                        context.getContentResolver().delete(Uri, Null, Null);  //This code clean the media libary from deleted file
///////////// This line clean the media library in ScanCompleted event /////////////////////
                    }
                });
    } Catch (Exception e) {
        e.printStackTrace();
    }
}

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim MediaScanner As JavaObject
   MediaScanner.InitializeStatic("android.media.MediaScannerConnection")
   'Create the interface
   Dim e As Object = MediaScanner.CreateEvent("android.media.MediaScannerConnection.OnScanCompletedListener", "scan", Null)
   MediaScanner.RunMethod("scanFile", Array As Object(GetContext, _
     Array As String(File.Combine(File.DirRootExternal, "1.jpg")), _
     Null, _
     e))
End Sub

Sub scan_Event (MethodName As String, Args() As Object) As Object
   Log("Path = " & Args(0))
   Log("Uri = " & Args(1))

///////////// This line clean the media library in ScanCompleted event /////////////////////
HERE ??? (JAVA to V4A) context.getContentResolver().delete(Uri, Null, Null);  //This code clean the media libary from deleted file
///////////// This line clean the media library in ScanCompleted event /////////////////////

   Return False
End Sub

Sub GetContext As Object
   Dim jo As JavaObject = Activity
   Return jo.RunMethod("getContext", Null)
End Sub
 
Top