Android Question CursorIndex Out Of Bounds Exception

Alexander Stolte

Licensed User
Hello great community!,

I have errors by sending video files over WLAN.

Error Function:

Sub GetPathFromContentResult(UriString As String) As String
  If UriString.StartsWith("/") Then Return UriString 'If the user used a file manager to choose the image
  Dim Cursor1 As Cursor
  Dim Uri1 As Uri
  Dim Proj() As String = Array As String("_data")
  Dim cr As ContentResolver
  If UriString.StartsWith("content://") Then
  Dim i As Int = UriString.IndexOf("%3A")
  Dim id As String = UriString.SubString(i + 3)
  Cursor1 = cr.Query(Uri1, Proj, "_id = ?", Array As String(id), "")
  Cursor1 = cr.Query(Uri1, Proj, "", Null, "")
  End If
  Cursor1.Position = 0
  Dim res As String
  res = Cursor1.GetString("_data")
  Return res
End Sub
Yes i Know "GetPathFromContentResult" is not the best.

and here is the error line:

 res = Cursor1.GetString("_data")
And the last but not the least, the full error log:

Installing file.
PackageAdded: package:b4a.WireSharing
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (wiresharing) Create **
** Service (wiresharing) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
main_getpathfromcontentresult (B4A line: 277)
res = Cursor1.GetString("_data")
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
    at android.database.AbstractCursor.checkPosition(
    at android.database.AbstractWindowedCursor.checkPosition(
    at android.database.AbstractWindowedCursor.getString(
    at android.database.CursorWrapper.getString(
    at anywheresoftware.b4a.sql.SQL$CursorWrapper.GetString(
    at b4a.WireSharing.main._getpathfromcontentresult(
    at b4a.WireSharing.main._cc_result(
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(
    at anywheresoftware.b4a.BA.raiseEvent(
    at anywheresoftware.b4a.BA$
    at anywheresoftware.b4a.BA.setActivityPaused(
    at b4a.WireSharing.main$
    at android.os.Handler.handleCallback(
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invoke(Native Method)
Thanks for helping me :)


Alexander Stolte

Licensed User

how is that by other files like .txt or .pdf?
Or is there a way to combine that? So you have a path and you can receive videos, pictures and other files?
So I do not have to write a thousand functions for the individual types :)

Alexander Stolte

Licensed User
My english is not the best and there may be times already to misunderstandings. Sry

But when I click in the FileBrowser and get the file, but there must be a path, otherwise the browser would not display this file or? :)


Staff member
Licensed User
GetPathFromContentResult is problematic and is not guaranteed to work.

You don't need to use it if you want to read data from the resource. You can open an InputStream with the parameters passed in the Result sub.
You will not be able to use it with AsyncStreams.WriteStream but you will be able to read data from the InputStream and write it to AsyncStreams with AsyncStreams.Write.

Alexander Stolte

Licensed User

I have thought about everything again.

So the user is clicked on a button, a file-manager opens. The user choose a Picture, video or other files...
Then the manager closes and in a label comes the absolute path to the file.

How can i do this with b4a?

I have read to all the topics, but I absolutely want to do it with an installed manager.

greetings :eek::rolleyes: