Android Question Strange result with saving files (Solved)

derez

Expert
Licensed User
Longtime User
I used Agraham's ManageExternalStorage class https://www.b4x.com/android/forum/t...ernal-external-storage-sdk-30.130411/#content to save and load common files by two applications. I can save to and read from Download folder, I see with file explorer the files that were saved by both apps, but each application sees only the files that were saved by it, not achieving the target to be able to use each other files.
It looks like the app stamp the saved file so that other apps will not see it...
Edit: The apps actually save 2 files every time - a .dat and a .png. The problem above is for the dat files, the pictures do appear in both apps. The reported error when trying to load the file by code (not through the dialog) is permission denied.
 

Attachments

  • Screenshot_20230114-224723.png
    Screenshot_20230114-224723.png
    319.4 KB · Views: 66
  • Screenshot_20230114-224743.png
    Screenshot_20230114-224743.png
    117.6 KB · Views: 62
  • Screenshot_20230114-224752.png
    Screenshot_20230114-224752.png
    108.4 KB · Views: 61
Last edited:

agraham

Expert
Licensed User
Longtime User
Sorry, but I don't understand what is happening there. The, presumably third party, File Explorer will be using that same permission to access Downloads so I have no idea why your apps can't also see all the files. If you are getting a permission denied exception it looks like you have not correctly acquired the permission.

I guess the pngs are visible as Android classes them as media files which seem to be visible to other apps.

What SDK are you targeting and what Android version is on the device?
 
Upvote 0

derez

Expert
Licensed User
Longtime User
I use pixel4A version 13, sdk is 33.
I save with the following sub:
B4X:
Private Sub Save_Click
    Dim fd As FileDialog
    fd.FileFilter = ""
    fd.FileFilter = ".dat" ' for example or ".jpg,.png" for multiple file types
    fd.FastScroll = True
    fd.FilePath = File.DirRootExternal & "/Download"  ' also sets ChosenName to an emtpy string
    Dim fda As Object = fd.ShowAsync("B4A File Dialog", "OK", "Cancel", "", bmp, False)
    Wait For (fda) Dialog_Result(ret As Int)
    fd.ChosenName = fd.ChosenName & ".dat"
    Log(fd.chosenname)
    If ret = -1 Then  'ok
        raf.Initialize(fd.FilePath, fd.ChosenName, False)
        raf.WriteObject(devlist,True,0)
        raf.Close
        Cover.Invalidate
        cnvs.Initialize(Cover)
        bmp = cnvs.bitmap
        Dim Out As OutputStream
        Out = File.OpenOutput(fd.FilePath, fd.ChosenName.Replace("dat","png"), False)
        bmp.WriteToStream(Out,100,"PNG")
        Out.Close
        Log("pic saved")
    End If
End Sub

I don't use your class but added the permission to the manifest. When I did run the class I always got the message to allow permission but it never actualy had it, so asked for it again and again every time I run.
Without the class the files are saved but as explained above - can be loaded only by the creating app.

The file explorer has been written by myself, its permissions are:
B4X:
AddManifestText(
<uses-permission
  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  android:maxSdkVersion="19" />
)
AddManifestText(
<uses-permission
  android:name="android.permission.READ_EXTERNAL_STORAGE"
  android:maxSdkVersion="19" />
)

AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />
)

Thanks for looking into this problem.
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
I'm ashamed to admit, such a foolish fault. In get_permission I had to change the package name , but didn't. Now everything works fine.
 
Last edited:
Upvote 0
Top