£50 for the first working solution. SDK30 Android/data read/write

pipocas

Member
Hi. Here my (incomplete & with some bugs) android\data file browser. Requires ContentChooser library to be selected.
B4A:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #AdditionalJar:androidx.documentfile:documentfile
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Private xui As XUI
    Private ion As Object            'Ignore
    Dim jo As JavaObject   
    Dim rootUri,fatherUri As Object
    Type myRec (name As String, isDir As Boolean, fileUri As Object)
End Sub

Sub Globals
    Dim browse As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    Dim j As JavaObject = GetBA
    
    ion = j.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.InitializeContext
    jo.RunMethod("getAndroidDataFolder",Null)
    browse.Initialize("myBrowse")
    browse.Color=Colors.DarkGray
    Activity.AddView(browse,0,0,100%x,100%y)
    
End Sub

Sub Activity_Resume

End Sub


Sub ion_Event (MethodName As String, Args() As Object) As Object
    Dim i As Intent
    Dim u As Uri
    
    i=Args(1)
    u.Parse(i.GetData)
    rootUri=u
    fatherUri=u
    Dim files As List = jo.RunMethod("ListFiles",Array(u))
    For Each rec As myRec In files
        browse.AddSingleLine2(rec.name,rec)
    Next
    Return Null
End Sub

Sub myBrowse_ItemClick (Position As Int, Value As Object)
    Dim r As myRec = Value
    
    If r.isDir Then
        browse.Clear
        Dim files As List = jo.RunMethod("ListFiles",Array(r.fileUri))
        r.fileUri = fatherUri
        fatherUri = r.fileUri
        r.isDir=False
        r.name=".."
        browse.AddSingleLine2("..",r)
        For Each rec As myRec In files
            browse.AddSingleLine2(rec.name,rec)
        Next
    Else
        If r.name.CompareTo("..")=0 Then
            Log(fatherUri)
            Dim files As List = jo.RunMethod("ListFiles",Array(fatherUri))
            r.fileUri = fatherUri
            r.isDir=False
            r.name=".."
            browse.Clear
            browse.AddSingleLine2("..",r)
            For Each rec As myRec In files
                browse.AddSingleLine2(rec.name,rec)
            Next
        End If
    End If
    
            
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub GetBA As Object
    Dim jo As JavaObject
    Dim cls As String = Me
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)
    Return jo.GetField("processBA")
End Sub



#if JAVA
    import android.os.storage.StorageManager;
    import android.content.Context;
    import android.content.ContentResolver;
    import android.content.Intent;
    import android.net.Uri;
    import androidx.documentfile.provider.DocumentFile;


    public static void getAndroidDataFolder(){
        
        Context context = processBA.context;
        StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
         int REQUEST_ACTION_OPEN_DOCUMENT_TREE = 101;
        
        Intent intent = sm.getPrimaryStorageVolume().createOpenDocumentTreeIntent();
        String startDir = "Android/data";

        Uri uri = intent.getParcelableExtra("android.provider.extra.INITIAL_URI");
        String scheme = uri.toString();
        scheme = scheme.replace("/root/", "/document/");
        startDir = startDir.replace("/", "%2F");
        scheme += "%3A" + startDir;
        uri = Uri.parse(scheme);
        intent.putExtra("android.provider.extra.INITIAL_URI", uri);
        processBA.startActivityForResult((anywheresoftware.b4a.IOnActivityResult)_ion, intent);   
    }
    
    public static java.util.List ListFiles(Uri uri){
        DocumentFile dir = DocumentFile.fromTreeUri(processBA.context, uri);
        DocumentFile[] fileListed = dir.listFiles();
        java.util.List <_myrec> data = new java.util.ArrayList<_myrec>();
        
        //data.Initialize();
        for(int i = 0;i < fileListed.length;i++){
             //BA.Log(fileListed[i].getName());
            _myrec r = new _myrec();
            r.name = fileListed[i].getName();
            r.isDir = fileListed[i].isDirectory();
            r.fileUri = fileListed[i].getUri();
            data.add( r );
        }
        return data;
    }   
#End If
 

aidymp

Well-Known Member
Licensed User
Longtime User
Well, after putting in the effort to supply a solution, and waiving payment, it would have been nice (nay courteous) to have some feedback as to whether it solved the problem or not !

Sorry
Well, after putting in the effort to supply a solution, and waiving payment, it would have been nice (nay courteous) to have some feedback as to whether it solved the problem or not !

My kids spilled drinks on my laptop. Its awaiting a new motherboard. I have been setting up an old desktop but dont get much time with the kids especially now its school holidays. I will make time tonight to install b4a on that, and get back to you...
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…