Android Question [SOLVED] How to save .jpg files so they are automatically available to default Photo app on targetSdkVersion = 29+

JackKirk

Well-Known Member
Licensed User
Longtime User
I have mongrelized the code here:

https://www.b4x.com/android/forum/threads/show-image-in-gallery-android-7-0.97662/#content

for a SDK 29+ environment where I have to use RuntimePermissions.GetSafeDirDefaultExternal to move media to external storage:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    Dim rp As RuntimePermissions

    File.Copy(File.DirAssets, "junk.jpg", rp.GetSafeDirDefaultExternal("Pictures"), "/junk.jpg")
    Dim FilePath As String = File.Combine(rp.GetSafeDirDefaultExternal("Pictures"), "/junk.jpg")

    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim MediaScannerConnection As JavaObject
    MediaScannerConnection.InitializeStatic("android.media.MediaScannerConnection")
    Dim interface As Object = MediaScannerConnection.CreateEventFromUI("android.media.MediaScannerConnection.OnScanCompletedListener", "ScanCompleted", Null)
    MediaScannerConnection.RunMethod("scanFile", Array(ctxt, Array As String(FilePath), Array As String("image/jpeg"), interface))

End Sub

Sub ScanCompleted_Event (MethodName As String, Args() As Object) As Object
    Log(Args(0))
    Log(Args(1))
    Return Null
End Sub

The Manifest is:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="20" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.

I can get the image into

/storage/emulated/0/Android/data/b4a.example/files/Pictures/junk.jpg

with no problems, the MediaScannerConnection code appears to run OK (as indicated by the log) but the image does not appear in the default Photo app (which seems to be called "Google Photos" - I have turned off syncing to the cloud.

I'm testing on a stock standard Pixel 3 running Android 11

Attached is zipped project.

Any help appreciated...
 

Attachments

  • xxx.zip
    24 KB · Views: 129

roumei

Active Member
Licensed User
I'm not sure that MediaScannerConnection will work on Android 29+. Have you tried Erel's code here?
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
I'm not sure that MediaScannerConnection will work on Android 29+. Have you tried Erel's code here?
I took this code and ran it with miniscule changes.

Works fine on SDK 23 device - this was never a problem

DOES NOT WORK on SDK 30 device - with targetSdkVersion="29" or targetSdkVersion="30"

I have attached the project zip for anyone who wants to have a go.

I am starting to get a bit desperate...
 

Attachments

  • yyy.zip
    24.4 KB · Views: 126
Upvote 0

roumei

Active Member
Licensed User
Your example yyy.zip works for me (Android 11 and targetSdkVersion=30) and I can see the file under Pictures:

Pictures.jpg
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Hold your horses...

My previous post is WRONG - the last code does work on an SDK 30 (Android 11) device.

My mistake was assuming my brand new Pixel 3 Gallery app would work like my old Samsung S5.

Of course it doesn't - it has a separate "folder" called "Libraries" in which my errant files were all nicely (hidden) lined up.

Apologies to all but at least we now know what works...

roumei - this post crossed yours in the ether...
 
Last edited:
Upvote 0
Top