Save Bitmap to and load from External Storage
//package jp.android.fukuoka.tegaky;
import java.io.File;
import java.io.FileOutputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;
class PictUtil {
public static File getSavePath() {
File path;
if (hasSDCard()) { // SD card
path = new File(getSDCardPath() + "/Tegaky/");
path.mkdir();
} else {
path = Environment.getDataDirectory();
}
return path;
}
public static String getCacheFilename() {
File f = getSavePath();
return f.getAbsolutePath() + "/cache.png";
}
public static Bitmap loadFromFile(String filename) {
try {
File f = new File(filename);
if (!f.exists()) { return null; }
Bitmap tmp = BitmapFactory.decodeFile(filename);
return tmp;
} catch (Exception e) {
return null;
}
}
public static Bitmap loadFromCacheFile() {
return loadFromFile(getCacheFilename());
}
public static void saveToCacheFile(Bitmap bmp) {
saveToFile(getCacheFilename(),bmp);
}
public static void saveToFile(String filename,Bitmap bmp) {
try {
FileOutputStream out = new FileOutputStream(filename);
bmp.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch(Exception e) {}
}
public static boolean hasSDCard() { // SD????????
String status = Environment.getExternalStorageState();
return status.equals(Environment.MEDIA_MOUNTED);
}
public static String getSDCardPath() {
File path = Environment.getExternalStorageDirectory();
return path.getAbsolutePath();
}
}
Related examples in the same category