package net.micode.notes.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BitmapUtils {

    public static Bitmap read(String path) {
        try {
            return BitmapFactory.decodeStream(ContextUtils.getContext().getContentResolver().openInputStream(UriUtils.getUriForFile(path)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static Bitmap write(Uri uri, File file) {
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(ContextUtils.getContext().getContentResolver(), uri);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file));
            return bitmap;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
