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:
The Manifest is:
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...
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...