Issue
I'm trying to use MediaStore API
to save an image but it always save the file with this name
1637955326427.jpg and the size of the file is 0 B.
this is the code that i use:
OutputStream fos;
ContentResolver resolver = getBaseContext().getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, "fileName.jpg");
contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
try {
fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),list.get(0).getDocfile().getUri());
if(bitmap!=null){
bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
list.get(0).getDocFile()
return a DocumentFile
also i tested this on API 28
Thanks in advance
NOTICE this image already exist on the External Storage
, just need to copy it.
EDIT: I delete this code fos = resolver.openOutputStream(imageUri);
was duplucate. I do realy apologize. the file work fine but steal wrong name.
Solution
I delete this code fos = resolver.openOutputStream(imageUri);
was duplicate. the image work fine but steal wrong name.
About the Wrong name and extension, i think that MediaStore
doesn't work fine with Android 9 and lower, because i tested the same code on Android 11 and it's working fine
Answered By - Abdellah_SADEV
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.