avatar
Untitled

Guest 128 18th Jul, 2024

MARKUP 2.71 KB
                                           
                         import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class MediaSaver {
    public static void saveMediaToStorage(Context context, Bitmap bitmap) {
        // Generating a file name
        String filename = System.currentTimeMillis() + ".jpg";

        // Output stream
        OutputStream fos = null;

        // For devices running android >= Q
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // Getting the contentResolver
            ContentResolver resolver = context.getContentResolver();

            // Content resolver will process the contentvalues
            ContentValues contentValues = new ContentValues();

            // Putting file information in content values
            contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, filename);
            contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
            contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);

            // Inserting the contentValues to contentResolver and getting the Uri
            Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

            // Opening an outputstream with the Uri that we got
            try {
                fos = resolver.openOutputStream(imageUri);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            // These for devices running on android < Q
            // So I don't think an explanation is needed here
            File imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            File image = new File(imagesDir, filename);
            try {
                fos = new FileOutputStream(image);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (fos != null) {
            // Finally writing the bitmap to the output stream that we opened
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            try {
                fos.close();
                context.toast("Saved to Photos");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void toast(Context context, String message) {
        // Implement your toast method here
    }
}
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data
Recent Pastes
Ta strona używa plików cookie w celu usprawnienia i ułatwienia dostępu do serwisu oraz prowadzenia danych statystycznych. Dalsze korzystanie z tej witryny oznacza akceptację tego stanu rzeczy.
Wykorzystywanie plików Cookie
Jak wyłączyć cookies?
ROZUMIEM