我需要保留有关图像的信息,这是用户最后一次工作。
在此之前,我已将完整图像保存在设备内存中。但这需要很长时间。
现在我决定保留有关图像的信息。但在我的应用程序中,我使用资源中的图像和图库中的图像。
如何存储图像信息以及如何在与应用程序的下一次会话期间使用该信息获取图像?
我失败的尝试:
public abstract class BitmapDescriptor {
protected static final String KEY_EXISTING_FLAG = "SavedBitmapDescriptor";
protected static final String KEY_DESCRIPTOR_TYPE = "DescriptorType";
protected static final String TYPE_IS_RESOURCE = "ResourceBitmapDescriptor";
protected static final String TYPE_IS_GALLERY = "ResourceBitmapDescriptor";
protected static final String KEY_RESOURCE_VALUE = "ValueOfResourceType";
protected static final String KEY_GALLERY_VALUE = "ValueOfGalleryType";
private static SharedPreferences pref;
public static BitmapDescriptor load(SharedPreferences pref) {
BitmapDescriptor.pref = pref;
if (notSavedDescriptor()) {
return null;
}
String descriptorType = getDesriptorType();
if (descriptorType.equals(TYPE_IS_RESOURCE)) {
return resourceDescriptor();
} else if (descriptorType.equals(TYPE_IS_GALLERY)) {
return galleryDescriptor();
} else {
throw new RuntimeException("BitmapDescriptor.load(): Cannot load descriptor.");
}
}
private static boolean notSavedDescriptor() {
return !pref.getBoolean(KEY_EXISTING_FLAG, false);
}
private static String getDesriptorType() {
return pref.getString(KEY_DESCRIPTOR_TYPE, "");
}
private static BitmapDescriptor resourceDescriptor() {
int resId = pref.getInt(KEY_RESOURCE_VALUE, -1);
return new ResourceBitmapDescriptor(resId);
}
private static BitmapDescriptor galleryDescriptor() {
String path = pref.getString(KEY_GALLERY_VALUE, "");
return new GalleryBitmapDescriptor(path);
}
public abstract Bitmap getBitmap();
public void save(SharedPreferences pref) {
Editor editor = pref.edit();
editor.putBoolean(KEY_EXISTING_FLAG, true);
save(editor);
editor.commit();
}
protected abstract void save(Editor editor);
}
public class ResourceBitmapDescriptor extends BitmapDescriptor {
private final int resId;
public ResourceBitmapDescriptor(int resId) {
this.resId = resId;
}
@Override
public Bitmap getBitmap() {
Resources resources = GlobalContext.get().getResources();
return BitmapFactory.decodeResource(resources, resId);
}
@Override
protected void save(Editor editor) {
editor.putString(KEY_DESCRIPTOR_TYPE, TYPE_IS_RESOURCE);
editor.putInt(KEY_GALLERY_VALUE, resId);
}
}
public class GalleryBitmapDescriptor extends BitmapDescriptor {
private final String path;
public GalleryBitmapDescriptor(String path) {
this.path = path;
}
@Override
public Bitmap getBitmap() {
File imgFile = new File(path);
return BitmapFactory.decodeFile(imgFile.getAbsolutePath());
}
@Override
protected void save(Editor editor) {
editor.putString(KEY_DESCRIPTOR_TYPE, TYPE_IS_GALLERY);
editor.putString(KEY_GALLERY_VALUE, path);
}
}
对不起我的英语不好。谢谢