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.
 

Alexander Stolte

Expert
Licensed User
Longtime User
And then to use the path then I need the: ContentResolver? If that then folds, then convert the file into bytes and then send to the other device, is the correct thinking of me?
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Can someone recommend a tutorial to send the files then with the Asynch to another device? :)

As I said, it is a new language for me, I am used to developing software and there is much different, I am still learning :) Even if I sometimes stupid instead of :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
In that case you need to copy it to a temporary file and then send it with AsyncStreams.WriteStream.
B4X:
Sub CC_Result (Success As Boolean, Dir As String, FileName As String)
If Success Then
  Dim f As String = File.DirInternalCache
  File.Copy(Dir, FileName, f, "tmp")
  AStream.WriteStream(File.OpenInput(f, "tmp"), File.Size(f, "tmp"))
  'make sure not to overwrite the file until the file was sent. The other device should send a message after it receives all data.
End If
End Sub

Important tutorial: https://www.b4x.com/android/forum/threads/72149/#content
 
Last edited:
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Either I have a mistake or I can not get what I'm missing. I miss a library?

Sub CC_Result (Success As Boolean, Dir As String, FileName As String)
If Success Then
Dim f As String = File.DirInternalCache
File.Copy(Dir, FileName, f, "tmp")
AStream.WriteString(
File.OpenInput(f, "tmp"), File.Size(f, "tmp"))
'make sure not to overwrite the file until the file was sent. The other device should send a message after it receives all data.
End If
End Sub

Unknown member: writestring

I have looked at the tutorial which you have recommended to me, but do not find the error.

My RandomAccessFile is the Version: 2.20
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Hi all,

Is that possible to send pictures or videos from WhatsApp with this code? Because i have an error.

B4X:
java.io.FileNotFoundException: content:/com.android.providers.media.documents/document/image%3A16464: open failed: ENOENT (No such file or directory)

I have the upper code on my program tuned and adjusted so he looks:

B4X:
Sub CC_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
                CallSubDelayed3(Main2, "SendFile","",FileName)
    Else
        If LastException.IsInitialized Then ToastMessageShow(LastException.Message, True)
    End If
End Sub

B4X:
Public Sub SendFile(Dir As String, FileName As String)
'    Dim totalSizeForSending As Long = File.Size(Dir, FileName)
'    Dim In As InputStream = File.OpenInput(Dir, FileName)
'    countingStream.Initialize(In)
'    currentFile = FileName.SubString(FileName.LastIndexOf("/") + 1)
    Dim f As String = File.DirInternalCache
    File.Copy(Dir, FileName, f, "tmp")
    'astream.Write(currentFile.GetBytes("UTF8")) 'write the file name
    astream.WriteStream(File.OpenInput(f, "tmp"), File.Size(f, "tmp"))
    'astream.WriteStream(countingStream, totalSizeForSending)
    'lblFile = "Sending: " & currentFile
    timer1.Enabled = True
    SendingFile = True
    UpdateProgress
End Sub

And here comes the error.
B4X:
  File.Copy(Dir, FileName, f, "tmp")
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
so I left him empty.
give the parameter you got. You got dir and then you discard it
B4X:
Sub CC_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
                CallSubDelayed3(Main2, "SendFile",Dir,FileName)
    Else
        If LastException.IsInitialized Then ToastMessageShow(LastException.Message, True)
    End If
End Sub

Try again ;)
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Try again ;)

It works fine thanks :)

Need help with the received.


The send and receive folds well, only is the received file for example a picture with .jpg with the receiving client then no image more but an unknown file. How can I save the type of the file, so I can save it properly?
 

Attachments

  • Screenshot_20170106-185346.png
    Screenshot_20170106-185346.png
    88.9 KB · Views: 301
Last edited:
Upvote 0
Top