Android Question Export SQLite to CSV File not Found

Andromeda

Member
Licensed User
Longtime User
Hello, i try to export with your Stringutil Code and then wants to start excel to read the csv file.
The debugger shows size of List1 = 40 so i have 40 Lines to inserted.
But the debugger then stops at the next line when it wants to CREATE the file:

FileNotFoundException
java.io.FileNotFoundException: /storage/emulated/0/Android/data/b4a.MyFirstProgram/files/Test.csv: open failed: ENOENT (No such file or directory)

Of course there should no such file....it should create it. Or is my thinking about it wrong?

Any hint would be great

B4X:
Dim su As StringUtils
    Dim Cursor1 As Cursor
    Cursor1 = Main.SQL1.ExecQuery("SELECT Nummer, Jobname, Titel FROM jobevents")
    Dim list1 As List
    list1.Initialize
    Dim cols() As String
    For i = 0 To Cursor1.RowCount - 1
        Cursor1.Position = i
        cols = Array As String(Cursor1.GetString2(0), Cursor1.GetString2(1), Cursor1.GetString2(2))
        list1.Add(cols)
    Next
    Cursor1.Close
    Log(list1.Size)
    su.SaveCSV(File.DirDefaultExternal, "Test.csv", ",", list1)
    
    Dim k As Intent
    k.Initialize(k.ACTION_VIEW, "file://" & File.DirDefaultExternal & "/Test.csv")
    k.SetType("application/csv")
    StartActivity(i)
 

kisoft

Well-Known Member
Licensed User
Longtime User
 
Upvote 0

Andromeda

Member
Licensed User
Longtime User
OK, sounds like android is now able to do the correct thing. After i change to
B4X:
su.SaveCSV(rp.GetSafeDirDefaultExternal(""), "Test.csv", ",", list1)


After that it stops at the

StartActivity(i)

and debugger says:
java.lang.ClassCastException: java.lang.Integer cannot be cast to android.content.Intent

Im stuck again
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
HI
Did you implement it?
 
Upvote 0

Andromeda

Member
Licensed User
Longtime User
I did add this line in my manifest

<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="19" />
 
Upvote 0

Andromeda

Member
Licensed User
Longtime User
After i changed i to k :) (sorry for this Noobfault)
i get a new error

android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/b4a.MyFirstProgram/files/Test.csv exposed beyond app through Intent.getData()
 
Upvote 0

Andromeda

Member
Licensed User
Longtime User
yes, already changed from I to K but the other error above rises.

I just want to write a CSV File and wants to give it to excel to show it
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
yes, already changed from I to K but the other error above rises.

I just want to write a CSV File and wants to give it to excel to show it
How looks your code now?
Have you also modified the building of the Intent?

su.SaveCSV(rp.GetSafeDirDefaultExternal(""), "Test.csv", ",", list1)
Use a Process_Globals variable:

B4X:
Private DirDefaultExternal As String
DirDefaultExternal = rp.GetSafeDirDefaultExternal("")

su.SaveCSV(DirDefaultExternal, "Test.csv", ",", list1)

    Dim k As Intent
    k.Initialize(k.ACTION_VIEW, "file://" & DirDefaultExternal & "/Test.csv") ' not sure about this, something like this!
    k.SetType("application/csv")
    StartActivity(k)
 
Upvote 0

Andromeda

Member
Licensed User
Longtime User
OK, now i tried it with the FileProviderClass...

B4X:
    Dim FileName As String = "Test.csv"
    File.Copy(DirDefaultExternal, FileName, Main.Provider.SharedFolder, FileName)
    Dim in As Intent
    in.Initialize(in.ACTION_VIEW, "")
    Main.Provider.SetFileUriAsIntentData(in, FileName)
    'Type must be set after calling SetFileUriAsIntentData
    in.SetType("application/csv")
    StartActivity(in)

But seems that Android not find Excel for that typ=application/csv

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://b4a.MyFirstProgram.provider/name/Test.csv typ=application/csv flg=0x20001 }

So now excel is the problem at the last line
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Try this one (found right now and it is very old; I'm still searching ?)
B4X:
Sub OpenExcel(FileName As String)
   Dim i As Intent 'Requires a reference to the Phone library
   i.Initialize(i.ACTION_VIEW, FileName)
   i.SetType("application/vnd.ms-excel")
   i.WrapAsIntentChooser("Choose Excel Viewer")
   StartActivity(i)
End Sub
 
Upvote 0
Top