Android Question [Solved] file.readstring error java.io.FileNotFoundException: No content provider: config.con

irda

Member
Licensed User
Longtime User
Hello, I have the following problem that I cannot find a solution.
I just want to select a file, read the content and get the data to process it, but the following code gives me an error when trying to read the file. This is my code:
B4X:
Dim cc As ContentChooser
cc.Initialize("cc")
cc.Show("*/*", "Seleccionar configuración")
Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)
If Success Then
    Dim parser As CSVParser
    parser.Initialize
        
    Dim resolver As ContentResolver
    resolver.Initialize("")
    Dim u As Uri
    u.Parse(FileName)
    Dim rs As ResultSet = resolver.Query(u, Null, "", Null, "")
    If rs.NextRow Then
        Dim columns As B4XSet = B4XCollections.CreateSet
        For i = 0 To rs.ColumnCount - 1
            columns.Add(rs.GetColumnName(i))
        Next
    End If

    MsgboxAsync("Dir: " & Dir & CRLF & "Filename: " & FileName, "")
    
    'Only filename'
    FileName = rs.GetString("_display_name")
    rs.Close
        
    Dim filestr As String
    'vv ERROR vv'
    filestr = File.ReadString(Dir, FileName)
        
    Dim data As List = parser.Parse(filestr, "#" , False)
        
end if

Dir = "ContentDir"
Filename = "content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Fconfig.con"
Filename after filter = "config.con"

But in line File.readstring(Dir, Filename") I catch the next error:

B4X:
running waiting messages (1)
Error occurred on line: 1068 (B4XInfoPage)
java.io.FileNotFoundException: No content provider: config.con
    at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1996)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1825)
    at android.content.ContentResolver.openInputStream(ContentResolver.java:1502)
    at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:211)
    at anywheresoftware.b4a.objects.streams.File.ReadString(File.java:276)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at b4a.example.b4xinfopage._procesar_fichero_configuracion(b4xinfopage.java:6681)
    at b4a.example.b4xinfopage$ResumableSub_mnuAcciones_Click.resume(b4xinfopage.java:6627)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.phone.Phone$ContentChooser$1.ResultArrived(Phone.java:865)
    at anywheresoftware.b4a.BA$4.run(BA.java:585)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:459)
    at b4a.example.main$ResumeMessage.run(main.java:313)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:236)
    at android.app.ActivityThread.main(ActivityThread.java:8056)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
(Exception) java.lang.Exception:  java.io.FileNotFoundException: No content provider: config.con

Any ideas? thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim cc As ContentChooser
cc.Initialize("cc")
cc.Show("*/*", "Seleccionar configuración")
Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)
If Success Then
 Dim s As String = File.ReadString(Dir, FileName)


End If

Better to wrap File.ReadString with Try / Catch as the user might choose a non-text file.
 
Upvote 0

irda

Member
Licensed User
Longtime User
Thank you very much for your prompt response, I really appreciate the help and time you dedicate to this community.

I always use try-catch to handle errors, but the code in the post is a simplification.

Your code is the first option I test with error. But the error seems to be from the emulator that I am using (LDPlayer), now it works correctly in a real terminal. Thanks for your help.
 
Upvote 0
Top