Android Question CursorIndex Out Of Bounds Exception

Discussion in 'Android Questions' started by Alexander Stolte, Jan 2, 2017.

  1. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    Hello great community!,

    I have errors by sending video files over WLAN.

    Error Function:

    Code:
    Sub GetPathFromContentResult(UriString As StringAs 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:

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

    Code:
    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.
     
  2. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    i see:

    Code:
    Uri1.Parse("content://media/external/images/media")
    "/images"

    Means that only pictures can be sent?
    Sry I unfortunately only afterwards seen :/
     
  3. DonManfred

    DonManfred Expert Licensed User

    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
     
  4. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    @DonManfred

    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 :)
     
  5. DonManfred

    DonManfred Expert Licensed 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
     
  6. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

  7. DonManfred

    DonManfred Expert Licensed User

    Ok, but this thread has nothing to do with asyncstreams! :D
     
  8. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    @DonManfred

    But in the title is:
    "FileTransfer - Send and receive files with AsyncStreams"

    why do you think that?
     
  9. DonManfred

    DonManfred Expert Licensed User

    CursorIndex Out Of Bounds Exception
     
  10. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    The error message tells me nothing, so I asked here for advice :(

    Or I have misunderstood which thread you mean.
     
  11. DonManfred

    DonManfred Expert Licensed User

    I maybe did not declrared exactly enough....
    I was not talking about the asyncstream-thread. I was talking about THIS thread here.
     
  12. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

  13. DonManfred

    DonManfred Expert Licensed User

    But you read the comment in the thread, yes?

    The interesting part is inside brackets. the path is NOT always available.
     
  14. Alexander Stolte

    Alexander Stolte Well-Known Member 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? :)
     
  15. Erel

    Erel Administrator 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 likes this.
  16. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed 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:
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    It is not always possible to get the path. The good thing is that you don't need the absolute path.
     
  18. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    @Erel

    And how can I start such a manager under android? In visual studio I have an open file dialog for this. Is there a library or tutorial for this?
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    Please don't limit your questions to a single member. This is a public forum.

    You should use Content Chooser.
     
  20. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    thanks, sry my misstake. Nevertheless thank you :)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice