Android Question File Selection issue with ExternalStorage example

Discussion in 'Android Questions' started by MikeJ, Jun 14, 2018.

  1. MikeJ

    MikeJ New Member Licensed 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
    [​IMG]
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Change the example code to:
    Code:
    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?
     
  3. MikeJ

    MikeJ New Member Licensed User

    The issue remains the same.
    The log produced is a follows:
    Code:
    *** 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
     
  4. Geezer

    Geezer Member Licensed User

    Code:
    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
     
    MikeJ, Erel and DonManfred like this.
  5. MikeJ

    MikeJ New Member Licensed 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
     
  6. Geezer

    Geezer Member Licensed 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.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Thank you @Geezer. I will update the example. This is the same issue as the issue written in the comment above. The ExternalFile returned from ListFiles cannot be used directly.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice