如何才能使我的位图传递给另一个活动?

时间:2012-03-08 09:19:42

标签: android

我有两个带有自己的gridviews和Adapters的活动,我想传递一个数据(包含图像(以位图资源的形式)和文本)从一个活动到另一个活动。我遇到的问题是图像。它不会在gridview上仅显示文本,也会在我尝试添加其他数据时覆盖前一个数据。请有人告诉我我做错了什么或我需要做什么? (我希望此应用程序的功能就像您将喜爱的在线广播电台添加到预设列表中一样)。 以下是活动(裁剪)

第一项活动

com_res=getResources();

    Comm_AppsList=new ArrayList<Appis_Infos>();
    Comm_AppsList.add(new Appis_Infos(BitmapFactory.decodeResource(com_res, R.drawable.skype), "Skype","com.skype.raider"));
    Comm_AppsList.add(new Appis_Infos(BitmapFactory.decodeResource(com_res, R.drawable.yahoomessenger),"Messenger","com.yahoo.mobile.client.android.im"));
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    int menuItemIndex = item.getItemId();

    String[] menuItems = getResources().getStringArray(R.array.menu);
    String menuItemName = menuItems[menuItemIndex];
    int menuappname=info.position;

        Appis_Infos AppName = Comm_AppsList.get(menuappname);
        Bitmap itemB=AppName.getIcon();//This is what I need as the image to the other class but when I use it in the Bitmapfactory I get an error


        Bitmap bmp = BitmapFactory.decodeResource(com_res, menuItemIndex);
        Intent favList=new Intent(this.getApplicationContext(),FavouriteListAppis.class);
        Bundle Bbundle=new Bundle();
        Bbundle.putParcelable("KeyB", bmp);

        favList.putExtra("KeyName", AppName.getName());
        startActivity(favList);
        Toast.makeText(getApplicationContext(), AppName.getName()+" has been added to your favourite list "+menuItemIndex, Toast.LENGTH_LONG).show();

    return true;
}
 class CommAppis_Adapter extends BaseAdapter{
private Context AIcontext;
private List<Appis_Infos>AdList;

CommAppis_Adapter(Context Aicontext, List<Appis_Infos> aDlist){
AIcontext=Aicontext;
AdList=aDlist;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return AdList.size();
}

@Override
public Object getItem(int it_position) {
    // TODO Auto-generated method stub
    return AdList.get(it_position);
}

@Override
public long getItemId(int id_position) {
    // TODO Auto-generated method stub
    return id_position;
}

@Override
public View getView(int Vposition, View Aview, ViewGroup Vparent) {
    // TODO Auto-generated method stub
    Appis_Infos appsData=AdList.get(Vposition);
    Comm_Viewholder holder=null;

    if(Aview==null){
        LayoutInflater apps_inflater=LayoutInflater.from(AIcontext);
        Aview=apps_inflater.inflate(R.layout.comm_appis, null);
        holder=new Comm_Viewholder();

        holder.apps_icons=(ImageView)Aview.findViewById(R.id.comm_imageview);
        holder.apps_name=(TextView)Aview.findViewById(R.id.comm_tvName);


        Aview.setTag(holder);
    }else{
        holder=(Comm_Viewholder)Aview.getTag();
    }

    holder.apps_icons.setImageBitmap(appsData.getIcon());
    holder.apps_name.setText(appsData.getName());

    return Aview;
}

}
class Comm_Viewholder{
ImageView apps_icons;
TextView apps_name;

}

第二项活动

Fav_gridview=(GridView)findViewById(R.id.gvfavist);
    Resources fav_res=getResources();

    if(getIntent().getExtras()!=null) {
        Bundle bundle = this.getIntent().getExtras();
        bmp = bundle.getParcelable("KeyB");

        favapp=getIntent().getExtras().getString("KeyName");


    }
    Fav_AppsList=new ArrayList<Appis_Infos>();
        Fav_AppsList.add(new Appis_Infos( bmp, favapp,"com.skype.raider"));



        class FavAppis_Adapter extends BaseAdapter{
private Context AIcontext;
private List<Appis_Infos>AdList;

FavAppis_Adapter(Context Aicontext, List<Appis_Infos> aDlist){
AIcontext=Aicontext;
AdList=aDlist;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return AdList.size();
}

@Override
public Object getItem(int it_position) {
    // TODO Auto-generated method stub
    return AdList.get(it_position);
}

@Override
public long getItemId(int id_position) {
    // TODO Auto-generated method stub
    return id_position;
}

@Override
public View getView(int Vposition, View Aview, ViewGroup Vparent) {
    // TODO Auto-generated method stub
    Appis_Infos appsData=AdList.get(Vposition);
    Fav_Viewholder holder=null;

    if(Aview==null){
        LayoutInflater apps_inflater=LayoutInflater.from(AIcontext);
        Aview=apps_inflater.inflate(R.layout.favlist_appis, null);
        holder=new Fav_Viewholder();

        holder.apps_icons=     (ImageView)Aview.findViewById(R.id.favlistimageview);
        holder.apps_name=(TextView)Aview.findViewById(R.id.favlisttvName);


        Aview.setTag(holder);
    }else{
        holder=(Fav_Viewholder)Aview.getTag();
    }

    holder.apps_icons.setImageBitmap(appsData.getIcon());
    holder.apps_name.setText(appsData.getName());

    return Aview;
}

}
class Fav_Viewholder{
ImageView apps_icons;
TextView apps_name;

}

他们的共同课

public class Appis_Infos {
private Bitmap AIcon;
private String Aname;
private String packname;

public Appis_Infos(Bitmap icon, String name, String pname){
    AIcon=icon;
    Aname=name;
    packname=pname;
}
public void setIcon(Bitmap icon){
    AIcon=icon;
}
public Bitmap getIcon(){
    return AIcon;
}
public void setName(String name){
    Aname=name;
}
public String getName(){
    return Aname;
}
public void setPname(String pname){
    packname=pname;
}
public String getPname(){
    return packname;
}
}

1 个答案:

答案 0 :(得分:0)

您可以简单地将Bitmap命名为静态。

然后创建一个类似

的方法
public static Bitmap getBitmap(){
    return bitmap;
}

然后你可以简单地从其他活动打电话, bitmapwantedclass.getBitmap();

希望有所帮助