package net.unverschaemt.pinfever; import android.content.Context; import android.content.ContextWrapper; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * Created by D060338 on 29.05.2015. */ public class AvatarHandler { public final static int COMPRESS_FACTOR = 30; public static Bitmap getBitmapFromAvatarURL(Bitmap avatarURL) { return null; } public static Bitmap loadAvatarFromStorage(Context context, String id) { ContextWrapper cw = new ContextWrapper(context); File directory = cw.getDir("imageDir", Context.MODE_PRIVATE); Bitmap b = null; try { File f = new File(directory, id + ".jpeg"); b = BitmapFactory.decodeStream(new FileInputStream(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } return b; } public static ByteArrayOutputStream convertBitmapToJPEG(Bitmap bitmap) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESS_FACTOR, bos); return bos; } public static File saveAvatarToStorage(Context context, Bitmap bitmap, String fileName) { ContextWrapper cw = new ContextWrapper(context); File directory = cw.getDir("imageDir", Context.MODE_PRIVATE); File f = new File(directory, fileName + ".jpeg"); try { f.createNewFile(); } catch (IOException e) { e.printStackTrace(); } //Convert bitmap to byte array ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESS_FACTOR, bos); byte[] bitmapdata = bos.toByteArray(); //write the bytes in file FileOutputStream fos = null; try { fos = new FileOutputStream(f); fos.write(bitmapdata); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return f; } public static Bitmap decodeUri(Context context, Uri selectedImage) throws FileNotFoundException { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(context.getContentResolver().openInputStream(selectedImage), null, o); // The new size we want to scale to final int REQUIRED_SIZE = 200; // Find the correct scale value. It should be the power of 2. int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1; while (true) { if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) { break; } width_tmp /= 2; height_tmp /= 2; scale *= 2; } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(selectedImage), null, o2); } }