Мне нужно сохранить информацию об изображении, которая была в последний раз, когда пользователь работал.Сохранить/Загрузить информацию об изображении (путь, resourceId) на устройство
До сих пор я сохранил полное изображение в памяти устройства. Но это занимает очень много времени.
Теперь я решил сохранить информацию об изображении. Но в моем приложении я использую изображения из ресурсов и изображений из галереи.
Как сохранить информацию об изображении и как получить изображение, используя информацию в течение следующего сеанса с приложением?
Моя неудачная попытка:
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);
}
}
Извините за мой английский. Благодаря