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