Android Question How to reuse a file URI from ContentChooser after my app is closed?

noeleon

Active Member
Licensed User
How to reuse a file URI from ContentChooser after my app is closed?

I save the URI and load it when the app starts but it no longer has permission to access the file.

Please help.
 

Attachments

  • yuri.zip
    10.3 KB · Views: 58
Solution
You cannot select a file directly from the SD card.
You have to copy the file in xui.defaulfolder.
Here is an example that works.

B4A:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Label1 As Label
    Private ImageView1 As ImageView
    Dim cc As ContentChooser
    Dim kvs As KeyValueStore
    Dim Pic As String
    
    Private mDir, mFileName As String
    'Private kvImg As Bitmap
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
    cc.Initialize("cc")
    kvs.Initialize(xui.DefaultFolder, "kvs.dat")
    mDir = xui.DefaultFolder
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1...

zed

Active Member
Licensed User
You cannot select a file directly from the SD card.
You have to copy the file in xui.defaulfolder.
Here is an example that works.

B4A:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Label1 As Label
    Private ImageView1 As ImageView
    Dim cc As ContentChooser
    Dim kvs As KeyValueStore
    Dim Pic As String
    
    Private mDir, mFileName As String
    'Private kvImg As Bitmap
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
    cc.Initialize("cc")
    kvs.Initialize(xui.DefaultFolder, "kvs.dat")
    mDir = xui.DefaultFolder
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")

    Pic = kvs.Get("Pic")
    If Pic = "null" Then
        Log("null")
    Else   
        Label1.Text = Pic
        ImageView1.Bitmap = LoadBitmap(mDir,Pic)
    End If
    

End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    cc.Show("image/*", "Choose image")
End Sub

Private Sub cc_Result (Success As Boolean, Dir As String, FileName As String)
    
    If Success Then
        Log("dir = "&Dir &CRLF&"  File :  "& FileName)
        ImageView1.Bitmap = LoadBitmap(Dir, FileName)
        
        mFileName = GetFileInfoByIndex("_display_name", FileName)
        kvs.Put("Pic", mFileName)
        Label1.Text = mFileName
        
        Dim InStr As InputStream = File.OpenInput("ContentDir",FileName)
        Dim OutStr As OutputStream = File.OpenOutput(mDir,mFileName,False)
        File.Copy2(InStr,OutStr)
        OutStr.Close
        Log("namekvs = "&kvs.Get("Pic"))
    Else
        MsgboxAsync("open file error","")
    End If

End Sub

Sub GetFileInfoByIndex(column As String, uri As String) As String
    
    Dim results As String
    Dim Cur As Cursor
    Dim Uri1 As Uri
    Dim cr As ContentResolver
    cr.Initialize("")

    'if viewing by gallery
    If uri.StartsWith("content://media/") Then
        Dim i As Int = uri.LastIndexOf("/")
        Dim id As String = uri.SubString(i + 1)
        Uri1.Parse(uri)
        Cur = cr.Query(Uri1, Null, "_id = ?", Array As String(id), Null)
        Cur.Position = 0
        If Cur.RowCount <> 0 Then
            For i = 0 To Cur.ColumnCount - 1
                If Cur.GetColumnName(i) <> Null Then
                    If Cur.GetColumnName(i) = column Then
                        results = Cur.GetString2(i)
                        Exit
                    End If
                End If
            Next
        End If
    Else
        Uri1.Parse(uri)
        Cur = cr.Query(Uri1, Null, Null, Null, Null)
        Cur.Position = 0
        If Cur.RowCount <> 0 Then
            For i = 0 To Cur.ColumnCount - 1
                If Cur.GetColumnName(i) <> Null Then
                    If Cur.GetColumnName(i) = column Then
                        results = Cur.GetString2(i)
                        Exit
                    End If
                End If
            Next
        End If
    End If
    
    Cur.Close
    
    Return results
    
End Sub
 

Attachments

  • uriTest.zip
    10.8 KB · Views: 58
Upvote 0
Solution
Top