2013-06-19 46 views
-1

我需要保留有关图像的信息,这是用户最后一次工作的时间。保存加载图像信息(路径,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); 
    } 
} 

对不起,我的英语水平。 感谢

回答

0

。在你的ResourceBitmapDescriptor类的save方法错误:

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