Android Question imgPicture.SetBackgroundImage

scarr

Member
Licensed User
Longtime User
Hi All,

Having a problem with imgPicture.SetBackgroundImage

I use this code to select a picture and then save the path and file name into a database

B4X:
Sub btnPicture_Click
    Dim Chooser As ContentChooser
    If Chooser.IsInitialized = False Then
        Chooser.Initialize("chooser")
    End If  
    Chooser.Show("image/*", "Select an image")
End Sub

Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        PictureName = FileName
        PictureDir = Dir
        Log(">" & PictureDir & ">" )
        Log(">" & PictureName & ">" )
        imgPicture.SetBackgroundImage(LoadBitmap(PictureDir,PictureName))
    Else
        ToastMessageShow("No image selected", True)
    End If
End Sub

This "imgPicture.SetBackgroundImage(LoadBitmap(PictureDir,PictureName))" works perfectly

I then retrieve the Path and filename and try to re-load the picture when moving through the database record and it fails!

B4X:
    PictureName =  Cursor1.GetString("PictureName")
    PictureDir =  Cursor1.GetString("PictureDir")
    If(PictureName <>"" ) Then
        Log(">" & PictureDir & ">" )
        Log(">" & PictureName & ">" )
        imgPicture.SetBackgroundImage(LoadBitmap(PictureDir,PictureName))
    End If
    Cursor1.Close

As you can see I output the Dir and filename to the log and they are identical to what was saved but I get this error

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

I am testing in the emulator when this fails, anyone any ideas?

Steve

P.S. These are the Dir and Filename from the log

>ContentDir>
>content://com.android.providers.media.documents/document/image%3A7>
 
Last edited:

scarr

Member
Licensed User
Longtime User
Thanks Erel, so your saying pic it up and move it to a folder of my choice or making?

B4X:
File.Copy(PicturePath, PictureName, MyFolder, PictureName)

Can this be done in the emulator without issues?
 
Upvote 0

scarr

Member
Licensed User
Longtime User
Well I tried to make a folder In activity_create

B4X:
        File.MakeDir(File.DirRootExternal,"Pics")

I then tried to move the picture when selected using chooser

B4X:
                File.Copy(PictureDir, PictureName, File.DirRootExternal & "/Pics/", PictureName)

But it said no such file or directory, it looked like it created the folder Pics, this is probably a syntax slash thing but I have tried everything I can think of, but I am sure this is just stupidity on my behalf.

Steve
 
Upvote 0

scarr

Member
Licensed User
Longtime User
OK, So I have been trying to copy the file to a folder created be me, here are the lines of code, it fails when trying to copy the file.

B4X:
        File.MakeDir(File.DirInternal,"Pics")
        Log(File.DirInternal &"/Pics")

Log shows /data/user/0/b4a.sqlitelight1/files/Pics

B4X:
        imgPicture.SetBackgroundImage(LoadBitmap(PictureDir,PictureName))
        Log(File.DirInternal & "/Pics")
        File.Copy(PictureDir, PictureName, File.DirInternal & "/Pics/", PictureName)

File.Copy line fails with this error

java.io.FileNotFoundException: /data/user/0/b4a.sqlitelight1/files/Pics/content:/com.android.providers.media.documents/document/image%3A7: open failed: ENOENT (No such file or directory)

Any ideas?

Steve
 
Upvote 0

scarr

Member
Licensed User
Longtime User
Sorry Erel I missed that question!

content://com.android.providers.media.documents/document/image%3A7

Thanks

Steve

P.S. The real image is called 1.jpg
 
Last edited:
Upvote 0

scarr

Member
Licensed User
Longtime User
So, I am now using a real phone and USB cable to avoid issues, is there anyway of getting the real file name of the picture using Contentchooser?

Using a real phone I get this back

B4X:
>content://com.android.providers.media.documents/document/image%3A5345>

and not the actual file name.

Steve
 
Last edited:
Upvote 0

scarr

Member
Licensed User
Longtime User
Thanks Erel, OK, is there any other control that I can use to select a picture and it return the file name, Ideally just want to pass a folder name and have it display the pictures for selection.

Thanks Steve
 
Upvote 0
Top