save bitmap file for share on android 10

Solutions on MaxInterview for save bitmap file for share on android 10 by the best coders in the world

showing results for - "save bitmap file for share on android 10"
Jonah
06 Nov 2019
1private void saveImage(Bitmap bitmap, @NonNull String name) throws IOException {
2    boolean saved;
3    OutputStream fos;
4
5    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
6        ContentResolver resolver = mContext.getContentResolver();
7        ContentValues contentValues = new ContentValues();
8        contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
9        contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
10        contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/" + IMAGES_FOLDER_NAME);
11        Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
12        fos = resolver.openOutputStream(imageUri);
13    } else {
14        String imagesDir = Environment.getExternalStoragePublicDirectory(
15                Environment.DIRECTORY_DCIM).toString() + File.separator + IMAGES_FOLDER_NAME;
16
17        File file = new File(imagesDir);
18
19        if (!file.exists()) {
20            file.mkdir();
21        }
22
23        File image = new File(imagesDir, name + ".png");
24        fos = new FileOutputStream(image)
25
26    }
27
28    saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
29    fos.flush();
30    fos.close();
31}
32