保存\将图像信息(路径,resourceId)加载到设备

时间:2013-06-19 12:47:53

标签: android

我需要保留有关图像的信息,这是用户上次工作的时间。

之前我已将完整图像保存在设备内存中。但这需要很长时间。

现在我决定保留有关图像的信息。但在我的应用程序中,我使用来自图库中资源和图像的图像。

如何在应用程序的下一次会话中使用信息存储图像信息以及如何获取图像?

我失败的尝试:

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);
    }
}

抱歉我的英文。 感谢

1 个答案:

答案 0 :(得分:0)

save课程的ResourceBitmapDescriptor方法存在错误:

    editor.putString(KEY_DESCRIPTOR_TYPE, TYPE_IS_RESOURCE);
    editor.putInt(KEY_GALLERY_VALUE, resId);

您使用了错误的密钥作为资源ID。它应该假定为KEY_RESOURCE_VALUE

    editor.putString(KEY_DESCRIPTOR_TYPE, TYPE_IS_RESOURCE);
    editor.putInt(KEY_RESOURCE_VALUE, resId);