Android Question cannot copy file when using open gallary

karyadi

Member
Licensed User
Longtime User
Hi All,

i try copy file image that i choose from Image Gallery.

when i debug Sub imgChooser_Result(Success As Boolean, Dir As String, FileName As String)
Dir is blank (Dir = '') and FileName = /storage/emulated/0/DCIM/Camera/IMG_20160525_142639.jpg

when i execute
File.Copy(Dir, FileName, File.DirRootExternal, "1.jpg")
it cannot copy the file

then i search from the forum to use file.copy2
but still it cannot copy the file because these is no Dir value, only have FileName


here is the code
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

Sub imgChooser_Result(Success As Boolean, Dir As String, FileName As String)
    Dim nil_scale As Double
    Dim gambar As Bitmap
    Dim s As StringFunctions
    Dim path1 As String
    If Success Then
        Dim realPath As String = GetPathFromContentResult(FileName)
        path1 = s.Right(realPath,5)
        If path1 <> "1.jpg" Then
            'If the user used a file manager to choose the image
            If FileName.StartsWith("/") Then
                Dim in As InputStream = File.OpenInput(Dir, FileName)              
                 Dim out As OutputStream
                out = File.OpenOutput(File.DirRootExternal, "1.jpg", False)
                File.Copy2(in, out)
            Else
                 File.Copy(Dir, FileName, File.DirRootExternal, "1.jpg")
            End If
        End If
    Else
        ToastMessageShow("No image selected", True)
    End If
End Sub

What is wrong with my code?
Please help me

Thanks
 

karyadi

Member
Licensed User
Longtime User
yes thank erel,
i use other way, to show image using

B4X:
Sub imgChooser_Result(Success As Boolean, Dir As String, FileName As String)
    Dim returntext As String
    Dim foundslash As Int
    If Success Then 
        If FileName.StartsWith("/") Then
            returntext = FileName
            foundslash = returntext.lastindexof("/")
            FileName1 = returntext.substring(foundslash + 1)
            Folder1 = returntext.substring2(0, foundslash)
            If File.Exists(Folder1,FileName1) = True Then
                ImageView2.Bitmap = LoadBitmap(File.DirAssets,"white.jpg")
                FitCenterBitmap(ImageView2,Folder1,FileName1)
            Else
                ToastMessageShow("File Not Found",False)
            End If
        Else
            Folder1 = Dir
            FileName1 = FileName
            ImageView2.Bitmap = LoadBitmap(File.DirAssets,"white.jpg")
            FitCenterBitmap(ImageView2,Dir,FileName)
        End If
        Main.FileNameFoto = ""
    Else
        ToastMessageShow("No image selected", True)
    End If
End Sub

rather than using copy file that open that file to image view.
and detect path that are using "/" at first path and split that path into folde and file name
or using File.DirDefaultExternal + Filename
 
Upvote 0
Top