Save/load Bitmap : Bitmap « 2D Graphics « Android






Save/load Bitmap

    

//package com.sofurry.favorites.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

class SubmissionStorage {

  public static Bitmap loadSubmissionImage(int id) {
    return loadIcon("image" + id);
  }

  public static void saveSubmissionImage(int id, Bitmap icon) {
    saveIcon("image" + id, icon);
  }

  public static void deleteSubmissionImage(int id) {
    FileStorage.deleteFile("image" + id);
  }

  private static Bitmap loadIcon(String filename) {
    FileInputStream is;
    Bitmap bitmap = null;
    try {
      is = FileStorage.getFileInputStream(filename);
      if (is != null && is.available() > 0) {
        bitmap = BitmapFactory.decodeStream(is);
      } else {
        Log.w("soFurryApp", "Can't load from external storage");
      }
    } catch (Exception e) {
      Log.e("soFurryApp", "error in loadIcon", e);
    }

    return bitmap;
  }

  private static void saveIcon(String filename, Bitmap icon) {
    FileOutputStream os;
    try {
      os = FileStorage.getFileOutputStream(filename);
      if (os != null) {
        icon.compress(CompressFormat.JPEG, 80, os);
      } else {
        Log.w("soFurryApp", "Can't save to external storage");
      }
    } catch (Exception e) {
      Log.e("soFurryApp", "error in saveIcon", e);
    }
  }

  public static String saveImageToGallery(String filename, Bitmap icon) {
    String fullfilepath = null;
    try {
      File path = new File(Environment.getExternalStorageDirectory()
          + "/FurryWallpapers");
      path.mkdirs();
      File file = new File(path, filename);
      fullfilepath = file.getAbsolutePath();
      Log.d("SF Wallpaper", "Saving image " + file.getAbsolutePath());
      // file.createNewFile();

      FileOutputStream os = new FileOutputStream(file);
      icon.compress(CompressFormat.JPEG, 80, os);
      os.close();
    } catch (Exception e) {
      Log.w("ExternalStorage", "Error writing file", e);
    }
    return fullfilepath;
  }

}

class FileStorage {

  private static Context context;

  public static FileOutputStream getFileOutputStream(String filename)
      throws IOException {
    File f = null;
    f = new File(context.getCacheDir() + "/" + filename);
    Log.d("FileStorage", "writing file " + f.getAbsolutePath() + " - "
        + filename);
    if (f.createNewFile() && f.canWrite()) {
      return new FileOutputStream(f);
    }
    return null;
  }

  public static FileInputStream getFileInputStream(String filename)
      throws FileNotFoundException {
    File f = null;
    f = new File(context.getCacheDir() + "/" + filename);
    if (f.canRead()) {
      Log.d("FileStorage", "reading file " + f.getAbsolutePath() + " - "
          + filename);
      return new FileInputStream(f);
    } else {
      Log.d("FileStorage", "Can't read file " + filename);
    }
    return null;

  }

  public static void deleteFile(String filename) {
    File f = new File(context.getCacheDir() + "/" + filename);
    if (f.canRead()) {
      f.delete();
    }
  }

  public static void clearFileCache(ArrayList<String> exceptionFilenames) {
    File dir = context.getCacheDir();
    if (dir != null && dir.isDirectory()) {
      try {
        for (File children : dir.listFiles()) {
          Log.d("FileStorage", "checking " + children.getName());
          if (exceptionFilenames != null
              && !exceptionFilenames.contains(children.getName())) {
            Log.d("FileStorage", "Deleting unbound file "
                + children.getName());
            children.delete();
          }
        }
      } catch (Exception e) {
        Log.e("FileStorage", "failed to clean cache", e);
      }
    }

  }

  public static void setContext(Context c) {
    context = c;
  }

}

   
    
    
    
  








Related examples in the same category

1.Using BitmapFactory to decode Resource
2.Capture and save to Bitmap
3.Bitmap size
4.Draw Bitmap on Canvas
5.Bitmap.createBitmap
6.Draw Bitmap on Canvas with Matrix
7.Create a Bitmap for drawing
8.Load Bitmap and Draw
9.Bitmap and RenderScript
10.Alpha Bitmap
11.Load Bitmap from InputStream
12.Bitmap Decode
13.Bitmap Mesh
14.Bitmap Pixels
15.Create a Bitmap
16.Bitmap.Config.ARGB_8888,Bitmap.Config.RGB_565, Bitmap.Config.ARGB_4444
17.Purgeable Bitmap
18.Create a bitmap with a circle
19.This activity demonstrates various ways density can cause the scaling of bitmaps and drawables.
20.Get the current system wallpaper, modifies it and sets the modified bitmap as system wallpaper.
21.Bitmap cache by WeakHashMap
22.Memory Cache for Bitmap
23.Load Bitmap from resource
24.Crop Bitmap
25.Create Scaled Bitmap
26.downloading a bitmap image from http and setting it to given image view asynchronously
27.Get Bitmap From Local Path
28.Get Bitmap From Bytes
29.Compress Bitmap
30.Resize Bitmap
31.captures given view and converts it to a bitmap
32.Save Bitmap and BitmapFactory.decodeFile
33.Generate a blurred bitmap from given one
34.Get Bitmap From Name
35.Load Bitmap with Context
36.Load Bitmap from InputStream
37.Get Bitmap from Url with HttpURLConnection
38.Rotate Bitmap
39.Get Texture From Bitmap Resource and BitmapFactory.decodeStream
40.Drawable to Bitmap
41.Save Bitmap to and load from External Storage
42.Get Image Bitmap From Url
43.Scale Bitmap
44.Unscaled Bitmap Loader
45.Save Bitmap to External Storage Directory
46.Compress and save Bitmap image
47.Bitmap downloading, processing
48.Rotate a Bitmap
49.Find components of color of the bitmap at x, y.
50.get Bitmap From Url
51.Draw Bitmap and Drawable
52.Rotate, transform Bitmap
53.Rotate specified Bitmap by a random angle. Scales the specified Bitmap to fit within the specified dimensions.
54.Loads a bitmap from the specified url.
55.Bitmap Refelection
56.Create a transparent bitmap from an existing bitmap by replacing certain color with transparent
57.Get Texture From Bitmap Resource
58.Bitmap Resize
59.bitmap to Byte
60.Flip Image
61.Scale an Image
62.Translate Image
63.Image Mirror
64.Moving an Image with Motion
65.Draw on Picture and save
66.Calculate optimal preview size from given parameters
67.generate Mipmaps For Bound Texture
68.Provides common tools for manipulating Bitmap objects.