the app i'm working on uses a File
as a target for photo capturing. this is executed externally by user selecting a camera-app using Intent(ACTION_IMAGE_CAPTURE)
. since upgrading build and target-sdk to 29, there have been a series of issues starting with the restriction on freely accessing files on external storage. the first change was to use one of either the application's private-cache directory, eg:
File.createTempFile("tempImage", ".jpg", context.cacheDir)
or the applications private external-storage directory:
File.createTempFile("tempImage", ".jpg", context.getExternalFilesDir(Environment.DIRECTORY_PICTURES))
in combination with FileProvider access in file_paths.xml
, eg:
<paths>
<external-path name="images" path="Pictures/" /><!-- prior to SDK-29, use public external storage -->
<external-files-path name="externalImages" path="Pictures/" />
<files-path name="internalImages" path="internalImages/"/>
<cache-path name="cache" path="/" />
</paths>
these work well now after being properly configured, however implementing "Save to Gallery" functionality, eg: notifying other apps of new images is no longer working on devices running Android-10
// use FileProvider to make this new photo publicly accessible
val shareableUri = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, newImage)
context.sendBroadcast(
Intent(ACTION_MEDIA_SCANNER_SCAN_FILE).apply { data = uris.externalUri }
)
this approach should work but doesn't, regardless of where the original image is saved (private-app-dir, cache-dir, external-private)
MediaScannerConnection.scanFile(context, arrayOf(newImage.absolutePath), arrayOf("image/jpeg")) { path: String, uri: Uri? ->
if (uri == null) {
throw IllegalStateException("media scan failed...")
} else {
// successful
}
}
Are there are new restrictions in Android's SDK-29 which necessitate a change in MediaScanning, specifically related to the way in which a (potentially) private image file is scanned? I noticed that MediaScanner
methods mostly expect a String
path instead of a URI, so this leads me to think that the new restrictions won't apply to it since its a system component.