如何在2个活动之间传递Bitmap数组

时间:2013-02-27 00:27:20

标签: android android-intent bitmap

我一直试图将位图数组传递给另一个Activity一周,但我还是不能这样做

public class HomePage extends Activity {
private GridView gridView;
public Bitmap[] mBitArray;
public Bitmap[] mBitArray5;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);
    gridView = (GridView) findViewById(R.id.GridView1);
    mBitArray = new Bitmap[7];
    try
    {
        //these images are stored in the root of "assets"
        mBitArray[0] = getBitmapFromAsset("Gallary/g1p1.jpg");
        mBitArray[1] = getBitmapFromAsset("Gallary/g1p2.jpg");
        mBitArray[2] = getBitmapFromAsset("Gallary/g1p3.jpg");
        mBitArray[3] = getBitmapFromAsset("Gallary/g1p4.jpg");
        mBitArray[4] = getBitmapFromAsset("Gallary/g1p5.jpg");
        mBitArray[5] = getBitmapFromAsset("Gallary/g1p6.jpg");
        mBitArray[6] = getBitmapFromAsset("Gallary/g2p1.jpg");
       mBitArray[7] = getBitmapFromAsset("g2p2.jpg");
        mBitArray[8] = getBitmapFromAsset("hd-01.jpg");
        mBitArray[9] = getBitmapFromAsset("hd-02.jpg");
        mBitArray[10] = getBitmapFromAsset("hd-03.jpg");
        mBitArray[11] = getBitmapFromAsset("hd-04.jpg");
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

     gridView.setAdapter(new ImageAdapter(this, mBitArray));
      gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int       position,
                long id) {

            Intent i = new Intent(getApplicationContext(),       FullImageActivity.class);
            i.putExtra("id", position);
            startActivity(i);           
            // TODO Auto-generated method stub

        }
    });
}



public Bitmap getBitmapFromAsset(String strName) throws IOException {
    AssetManager assetManager = getAssets();
    InputStream istr = assetManager.open(strName);
    Bitmap bitmap = BitmapFactory.decodeStream(istr);
    istr.close();
    return bitmap;

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_home_page, menu);
    return true;
}

}






 public class ImageAdapter extends BaseAdapter {
 private Context mContext;
 private Bitmap[] mImageArray;


public ImageAdapter(Context context, Bitmap[] imgArray)
{
    mContext = context;
    mImageArray = imgArray;
}

public int getCount() {

    return mImageArray.length;
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return mImageArray[position];
}


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

public View getView(int position, View convertView, ViewGroup parent) {         
    ImageView imageView = new ImageView(mContext);
    imageView.setImageBitmap(mImageArray[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(153, 150));
    return imageView;
}

public Bitmap getView1(int position) throws IOException {   

    AssetManager am = mContext.getAssets();
    String[] list = am.list("Gallary");
     BufferedInputStream buf = new               BufferedInputStream(am.open(list[position]));

     Bitmap bitmap = BitmapFactory.decodeStream(buf);

 buf.close();
    return bitmap;
}

}

公共类FullImageActivity扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullimage);
    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
     Intent i = getIntent();
     int position = i.getExtras().getInt("id");


     ImageAdapter imageAdapter = new ImageAdapter(this , null);


    //imageView.setImageBitmap(mBitArray[position]);
    trying to get mBitarray to fullscreenactivity to display a certain picture from the bitmap array

所以我想将MbitArray变量传递给Fullactivity类我试图通过Intent来实现它,但我不能通过构造函数或内部类发送Bitmap数组?

1 个答案:

答案 0 :(得分:4)

由于Bitmap实现了Parcelable,并且你有一个Bitmap数组,你应该可以将它们传递掉:

Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
i.putExtra("bitmaps", mBitArray);
startActivity(i);  

然后通过getParcelableArrayExtra():

进行检索
Intent i = getIntent();
Bitmap [] bitmaps = (Bitmap[]) i.getParcelableArrayExtra ("bitmaps");

您还可以将String数组中的路径传递给FullImageActivity,然后在那里重新制作它们,就像在第一个Activity中一样。要保存代码,如果选择这种方法,通过将方法设为静态来共享该方法将是一个好主意,只需记住传递一个Context实例,这样就可以使用getAssets()

public static Bitmap getBitmapFromAsset(Context context, String strName) throws IOException {
    AssetManager assetManager = context.getAssets();
    InputStream istr = assetManager.open(strName);
    Bitmap bitmap = BitmapFactory.decodeStream(istr);
    istr.close();
    return bitmap;
}

然后从HomePage

致电
mBitArray[0] = getBitmapFromAsset(this,"Gallary/g1p1.jpg");

FullImageActivity

otherBitmapArray[0] = HomePage.getBitmapFromAsset(this, bitmapPaths[0]);