Android Question File Selection issue with ExternalStorage example

MikeJ

New Member
Licensed User
Longtime User
Hi,
I'm running Erel's example ExternalStorage code contained here:
https://www.b4x.com/android/forum/t...access-sd-cards-and-usb-sticks.90238/#content

and am having pretty much the same issue as described in post #4 here:
https://www.b4x.com/android/forum/threads/externalstorage.91271/

My steps are:
1. Give permission to the physically external SanDisk SD card
2. Go to Root/DCIM/Camera where I have 500+ images stored, then regardless of the image selected, the same image is presented. (which is an image in this folder)
If I select another folder that contains multiple images, only one image from that folder is presented in ImageView1 regardless of the image selected.

Running B4A v8.30 and installing on a MotoZ Play running Android 8.0.0 and an Xperia XZ1 also running Android 8.0.0

Any thoughts much appreciated

Regards,

Mike
cleardot.gif
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change the example code to:
B4X:
Private Sub ListView1_ItemClick (Position As Int, Value As Object)
   Dim f As ExternalFile = Value
   If f = UpItem Then
       'remove the current folder
       FoldersStack.RemoveAt(FoldersStack.Size - 1)
       'get the parent folder which is now the topmost folder
       Dim folder As ExternalFile = GetCurrentFolder
       'remove it and enter it again
       FoldersStack.RemoveAt(FoldersStack.Size - 1)
       EnterFolder(folder)
       
   Else
       If f.IsFolder Then
           'The ExternalFile returned from ListFiles cannot be used directly.
           'We need to first call FindFile.
           Dim folder As ExternalFile = Storage.FindFile(GetCurrentFolder, f.Name)
           EnterFolder(folder)
       Else If IsImageFile(f.Name) Then
           Log("***************")
           Log(f)
           Log(f.Name)
           Dim in As InputStream = Storage.OpenInputStream(f)
           File.Delete(File.DirInternal, "temp")
           'We can open the image directly with Bitmap.Initialize2 however it will not allow us to use LoadBitmapResize
           'so instead we copy it to a temporary file.
           Dim out As OutputStream = File.OpenOutput(File.DirInternal, "temp", False)
           File.Copy2(in, out)
           out.Close
           ImageView1.SetBackgroundImage(LoadBitmapResize(File.DirInternal, "temp", ImageView1.Width, ImageView1.Height, True))
       End If
   End If
End Sub

What happens when you select different images?
 
Upvote 0

MikeJ

New Member
Licensed User
Longtime User
The issue remains the same.
The log produced is a follows:
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
(HierarchicalUri) content://com.android.externalstorage.documents/tree/36323339-2d36-3633-3100-000000040000%3A
** Activity (main) Resume **
***************
[IsFolder=false, IsInitialized=true, LastModified=1515295360000
, Length=3915216, Name=IMG_20180107_112240.jpg, Native=(TreeDocumentFile) android.support.v4.provider.TreeDocumentFile@ee47a58
]
IMG_20180107_112240.jpg
***************
[IsFolder=false, IsInitialized=true, LastModified=1515319088000
, Length=2614581, Name=IMG_20180107_175808.jpg, Native=(TreeDocumentFile) android.support.v4.provider.TreeDocumentFile@ee47a58
]
IMG_20180107_175808.jpg
***************
[IsFolder=false, IsInitialized=true, LastModified=1515319132000
, Length=2034929, Name=IMG_20180107_175852.jpg, Native=(TreeDocumentFile) android.support.v4.provider.TreeDocumentFile@ee47a58
]
IMG_20180107_175852.jpg
The image presented in the imageview remains the same, being an image called IMG_20180310_153320.jpg. At no stage has this image been selected, and even if this example is uninstalled, reinstalled, and a random image is selected from this folder, this is the image that is presented in the imageview.

Regards
Mike
 
Upvote 0

Geezer

Active Member
Licensed User
Longtime User
B4X:
Private Sub ListView1_ItemClick (Position As Int, Value As Object)
    Dim f As ExternalFile = Value
    If f = UpItem Then
        'remove the current folder
        FoldersStack.RemoveAt(FoldersStack.Size - 1)
        'get the parent folder which is now the topmost folder
        Dim folder As ExternalFile = GetCurrentFolder
        'remove it and enter it again
        FoldersStack.RemoveAt(FoldersStack.Size - 1)
        EnterFolder(folder)
       
    Else
        If f.IsFolder Then
            'The ExternalFile returned from ListFiles cannot be used directly.
            'We need to first call FindFile.
            Dim folder As ExternalFile = Storage.FindFile(GetCurrentFolder, f.Name)
            EnterFolder(folder)
        Else If IsImageFile(f.Name) Then

' THIS LINE ADDED
            Dim f As ExternalFile = Storage.FindFile(GetCurrentFolder, f.Name)

            Dim in As InputStream = Storage.OpenInputStream(f)
            'We can open the image directly with Bitmap.Initialize2 however it will not allow us to use LoadBitmapResize
            'so instead we copy it to a temporary file.
            Dim out As OutputStream = File.OpenOutput(File.DirInternal, "temp", False)
            File.Copy2(in, out)
            out.Close
            ImageView1.SetBackgroundImage(LoadBitmapResize(File.DirInternal, "temp", ImageView1.Width, ImageView1.Height, True))
        End If
    End If
End Sub
 
Upvote 0

MikeJ

New Member
Licensed User
Longtime User
Thank you Geezer, that has solved the symptom for me.

I'm not understanding the need for that line, shouldn't f already contain the file location data?

Regards
Mike
 
Upvote 0

Geezer

Active Member
Licensed User
Longtime User
I had the same problem writing a media player, i'm not sure why it does that, but it points to the last file without that line.
 
Upvote 0
Top