Android Question CursorIndex Out Of Bounds Exception

Alexander Stolte

Expert
Licensed User
Longtime User
Hello great community!,

I have errors by sending video files over WLAN.

Error Function:

B4X:
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
  cr.Initialize("")
  If UriString.StartsWith("content://com.android.providers.media.documents") Then
  Dim i As Int = UriString.IndexOf("%3A")
  Dim id As String = UriString.SubString(i + 3)
  Uri1.Parse("content://media/external/images/media")
  Cursor1 = cr.Query(Uri1, Proj, "_id = ?", Array As String(id), "")
  Else
  Uri1.Parse(UriString)
  Cursor1 = cr.Query(Uri1, Proj, "", Null, "")
  End If
  Cursor1.Position = 0
  Dim res As String
  res = Cursor1.GetString("_data")
  Cursor1.Close
  Return res
End Sub

Yes i Know "GetPathFromContentResult" is not the best.

and here is the error line:

B4X:
 res = Cursor1.GetString("_data")

And the last but not the least, the full error log:

B4X:
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 **
StartAStream
** 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(AbstractCursor.java:460)
    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
    at android.database.CursorWrapper.getString(CursorWrapper.java:137)
    at anywheresoftware.b4a.sql.SQL$CursorWrapper.GetString(SQL.java:355)
    at b4a.WireSharing.main._getpathfromcontentresult(main.java:927)
    at b4a.WireSharing.main._cc_result(main.java:812)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.phone.Phone$ContentChooser$1.ResultArrived(Phone.java:843)
    at anywheresoftware.b4a.BA$4.run(BA.java:513)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:398)
    at b4a.WireSharing.main$ResumeMessage.run(main.java:300)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7229)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Thanks for helping me :)

greetings.
 

DonManfred

Expert
Licensed User
Longtime User
Means that only pictures can be sent?
you do not send anything. you are requesting results from the conentresolver.

I guess content://media/external/video is the way to go to get a list of videos
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Androids Gallery does not provide indexes for txt pdf or other files other than PICTURES and VIDEOS

What exactly you want archieve?

Edit: You can surely list all files of all folders of your internal or external sdcard (or even search for *.txt) using my Wildcard-class
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime 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? :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
@Erel

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:
 
Upvote 0
Top