Italian Copiare un file presente in cartella Download

Sabotto

Well-Known Member
Licensed User
Buondi. Posso copiare un file presente nella cartella Download?
Se si come?

Avevo provato con questo codice semplice innanzitutto a trovarlo, ma il file non me lo vede.
B4X:
Dim downloadDir As String = File.Combine(File.DirRootExternal, "Download")
Dim files As List
files.Initialize

'--- recupera solo file .db
For Each f As String In File.ListFiles(downloadDir)
    Log(f)
    If f.ToLowerCase.EndsWith(".db") Then
        files.Add(f)
    End If
Next
 

Sabotto

Well-Known Member
Licensed User
anche con contentchooser non riesco, mi fa scegliere il file .db, ma poi non me lo restituisce con il nome reale bensi con qualcosa tipo:
"content://com.android.providers.downloads.documents/document/msf%3A1000083612"

B4X:
Private destdir As String = GL.SQLDataBasePath
   
    cc.Show("application/octet-stream", "Scegli un file .db")
    Wait For cc_Result (Success As Boolean, Dir As String, FileName As String)
   
    If Success = False Then Return
   
    If FileName.ToLowerCase.EndsWith(".db") = False Then
        ToastMessageShow("Non ha estensione .db", True)
        Return
    End If

    If File.Exists(destdir, FileName) Then
        File.Delete(destdir, FileName)
    End If

    File.Copy(Dir, FileName, destdir, FileName)
    ToastMessageShow("File copiato: " & FileName, True)
 

Filippo

Expert
Licensed User
Longtime User
anche con contentchooser non riesco, mi fa scegliere il file .db, ma poi non me lo restituisce con il nome reale bensi con qualcosa tipo:
"content://com.android.providers.downloads.documents/document/msf%3A1000083612"
Prova in questo modo:
B4X:
Private destdir As String = GL.SQLDataBasePath
  
    cc.Show("application/octet-stream", "Scegli un file .db")
    Wait For CC_Result (Success As Boolean, Dir As String, uriFileName As String)
    If Success Then
        Dim FileName As String = GetFileInfoByIndex("_display_name", uriFileName)
        If FileName.ToLowerCase.EndsWith(".db") = False Then
           ToastMessageShow("Non ha estensione .db", True)
           Return
       End If

      If File.Exists(destdir, FileName) Then
         File.Delete(destdir, FileName)
     End If

    File.Copy(Dir, FileName, destdir, FileName)
   ToastMessageShow("File copiato: " & FileName, True) 
End Sub

Private 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
 

Sabotto

Well-Known Member
Licensed User
arriva fino a qui:
B4X:
File.Copy(Dir, FileName, destdir, FileName)
ma poi dà errore come se non trovasse il file (che si chiama db_benzina.db)

java.io.FileNotFoundException: No content provider: db_benzina.db
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:2044)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1873)
at android.content.ContentResolver.openInputStream(ContentResolver.java:1534)
...ecc ecc
 

Filippo

Expert
Licensed User
Longtime User
File.Copy(Dir, FileName, destdir, FileName)
Non si può usare direttamente la funzione "copy", in questo caso si deve usare questa funzione:
B4X:
        Dim Out As OutputStream
        Out = File.OpenOutput(File.DirInternal, FileName, False)
        Dim Inp As InputStream = File.OpenInput(Dir , uriFileName)
        File.Copy2(Inp, Out)
        Out.Close
 

Sabotto

Well-Known Member
Licensed User
Grazie Filippo. Funziona!
Ti rubo un'ultima info: Nell'ultimo blocco (quello con OutputStream) se volessi cambiare il nome del file destinazione rispetto al file target, dove dovrei agire?
 

Filippo

Expert
Licensed User
Longtime User
Nell'ultimo blocco (quello con OutputStream) se volessi cambiare il nome del file destinazione rispetto al file target, dove dovrei agire?
B4X:
Out = File.OpenOutput(File.DirInternal, FileName_quello_che_vuoi, False)
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…