Solution 1 :
I’m using this library to GIFEncoder, to generate a GIF file from images
That library is a bit old.
This library required a path at the initialization in order
Use a File
based off of methods on Context
, such as getFilesDir()
or getExternalFilesDir()
. Then, after the GIF is created, later move it to a public directory. Or, find a library that supports an OutputStream
or Uri
as a destination to write to.
Problem :
I’m using this library to GIFEncoder, to generate a GIF file from images, this library required a path at the initialization in order to save the final GIF with it as follows :
gifEncoder.init(width, height, FILEPATH , GifEncoder.EncodingType.ENCODING_TYPE_NORMAL_LOW_MEMORY);
for that I tried to create a GIF file and give the library the path:
String gifFileName = "APP" + Constants.CURRENT_DATE + ".gif";
String pictureFolderPath = Environment.DIRECTORY_PICTURES + Constants.BaseFolder;
String pathImageKey = MediaStore.Images.Media.DATA;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Uri insertedUri = getGIFUri(context, gifFileName, pictureFolderPath);
String correctPath = queryFilePath4Uri(context, insertedUri, pathImageKey);
Log.d("Correct Path", correctPath.toString());
return correctPath;
}
in log cat I can see the path :
2020-06-07 11:11:48.878 7570-7745/maa.app.abc D/Correct Path: /storage/emulated/0/Pictures/MYAPP/GIFs/APP20200607_101138.gif
but when I test it on Android 10 (Emulator), nothing happens the saved GIF becomes empty with 0 octets as size, and here’s the getGIFUri
and queryFilePath4Uri
methods :
fun getGIFUri(context: Context, fileName: String, dirName: String): Uri? {
val values = ContentValues().apply {
put(MediaStore.Images.Media.TITLE, fileName)
put(MediaStore.Images.Media.DISPLAY_NAME, fileName)
put(MediaStore.Images.Media.DESCRIPTION, "APP")
put(MediaStore.Images.Media.MIME_TYPE, "image/gif")
put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis())
put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis())
put(MediaStore.Images.Media.RELATIVE_PATH, "$dirName/GIFs")
}
var result: Uri? = null
val contentResolver = context.contentResolver
contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values).let { insertUri ->
result = insertUri
}
return result;
}
fun queryFilePath4Uri(context: Context, uri: Uri, pathKey: String): String? {
context.contentResolver?.query(uri, null, null, null, null)?.use {
val isHas = it.moveToFirst()
if (isHas) {
val dataIndex = it.getColumnIndex(pathKey)
if (dataIndex != -1) {
val data = it.getString(dataIndex)
if (!TextUtils.isEmpty(data)) {
return data
}
}
}
}
return uri.path
}
Comments
Comment posted by blackapps
If you need a file system path then dont use the media store but getExternalFilesDir(). The path you used is not readable and writable under Android Q.
Comment posted by blackapps
queryFilePath4Uri()
Comment posted by Mouaad Abdelghafour AITALI
Hi @blackapps thank you so much for your comment, can you please show me an example if you could
Comment posted by blackapps
Jus use getExternalFilesDir() as directory for your file. Not the one you have now.
Comment posted by Mouaad Abdelghafour AITALI
@blackapps it doesn’t mean the file will be created in the cache?, because I want to save the GIF file to the gallery later
Comment posted by Mouaad Abdelghafour AITALI
Thank you so much for your answer, it’s possible to move a file from getExternalFilesDir() path to the gallery ?
Comment posted by this sample app
@MouaadAbdelghafourAITALI: I would not use “move” as the verb. On Android 10+, you can store images by way of the
Comment posted by CommonsWare
@MouaadAbdelghafourAITALI: If you had a more modern GIF library that could accept an