2014-04-12 21 views
1

我有一个班级用于管理我的应用程序中的位图。 它需要传递给它的上下文才能使用getResources()。 问题是我相信你不能在Parcelable类中使用Context。安卓系统 - 替代使用上下文的内容

任何人有一个解决方案的任何想法? 下面是我的代码:

public class ImageManager implements Parcelable { 

    private static final long serialVersionUID = 66; 
    private HashMap<Integer, Bitmap> mBitmaps; 
    private HashMap<Integer, Drawable> mDrawables; 
    //private Context mContext; 

    private boolean mActive = true; 

    public ImageManager(Context c) { 
     mBitmaps = new HashMap<Integer, Bitmap>(); 
     mDrawables = new HashMap<Integer, Drawable>(); 
     //mContext = c; 
    } 

    public ImageManager(Parcel in) { 
     // TODO Auto-generated constructor stub 
    } 


    public Bitmap getBitmap(int resource) { 
     if (mActive) { 
      if (!mBitmaps.containsKey(resource)) { 
       mBitmaps.put(resource, 
        BitmapFactory.decodeResource(mContext.getResources(), resource)); 
      } 
      return mBitmaps.get(resource); 
     } 
     return null; 
    } 

    public Drawable getDrawable(int resource) { 
     if (mActive) { 
      if (!mDrawables.containsKey(resource)) { 
       mDrawables.put(resource, mContext.getResources().getDrawable(resource)); 
      } 
      return mDrawables.get(resource); 
     } 
     return null; 
    } 

    public void recycleBitmaps() { 
     Iterator itr = mBitmaps.entrySet().iterator(); 
     while (itr.hasNext()) { 
      Map.Entry e = (Map.Entry)itr.next(); 
      ((Bitmap) e.getValue()).recycle(); 
     } 
     mBitmaps.clear(); 
    } 

    public ImageManager setActive(boolean b) { 
     mActive = b; 
     return this; 
    } 

    public boolean isActive() { 
     return mActive; 
    } 

    @Override 
    public int describeContents() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     // TODO Auto-generated method stub 
     dest.writeValue(mBitmaps); 
     dest.writeValue(mDrawables); 
     //dest.writeValue(mContext); 
     dest.writeByte((byte) (mActive ? 0x01 : 0x00)); 

    } 

    public static final Parcelable.Creator<ImageManager> CREATOR = new Parcelable.Creator<ImageManager>() { 
     public ImageManager createFromParcel(Parcel in) { 
      return new ImageManager(in); 
     } 

     public ImageManager[] newArray(int size) { 
      return new ImageManager[size]; 
     } 
    }; 


} 
+0

你做错了什么。一个“经理”通常不能是可Parcelable或Serializable。 –

+0

同意。你为什么首先做这个“Parcelable”?除此之外,欢迎您添加一个'setContext()'setter,以便收件人在收到时可以附加一个'Context'。 – CommonsWare

+0

这样我就可以将它传递给其他活动,以便他们可以访问位图。 – user1290717

回答

0

您可以创建自己的Application实现,添加静态吸气。

public class Application extends android.app.Application { 

    private static Context context; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     context = this; 
    } 

    public static Context getContext(){ 
     return context; 
    } 
} 

而且在任何地方使用它,你需要:

Application.getContext(); 

但总的来说我的东西它不是最好的主意。

+0

Application.getContext()为我返回null。 – user1290717

+0

@ user1290717我想你没有创建自己的'Application'类。 – eleven

+0

是的,我创建它,并在调试该类。我发现“this”为空。 – user1290717

2

A“经理”通常不能被Parcelable或序列化。

Parcelable或序列化对象是那种只是抱着一些数据,并且不对其执行任何操作,并不需要参考上下文对象。

当你传递对象作为Parcelable,收到的目标将被重新创建这样这样,你不能保持相同的实例。

为了解决你的任务,做出的ImageManager单身。

public final class ImageManager { 

    public static synchronized ImageManager getInstance(final Context context) { 
     if (sInstance == null) { 
      sInstance = new ImageManager(context.getApplicationContext()); 
     } 
     return sInstance; 
    } 

    private static ImageManager sInstance; 

    private final Context mContext; 

    private ImageManager(Context c) { 
     mBitmaps = new HashMap<Integer, Bitmap>(); 
     mDrawables = new HashMap<Integer, Drawable>(); 
    } 

,只要你需要它的活动,呼吁

ImageManager imageManager = ImageManager.getInstance(this); 

这样相同的ImageManager将在所有活动进行访问。

0

我建议把你的类变成一个辅助类的让你需要static的方法,然后通过Context作为参数,以及像这样:

public Bitmap getBitmap(int resource, Context context) { ... }

不要存储参考Context在你的助手课,如果你这样做,你很可能会泄漏它。