如何通过2 activies之间的位图阵列 [英] how to pass Bitmap array between 2 activies

查看:105
本文介绍了如何通过2 activies之间的位图阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图位图数组传递到另一个活动了一个星期,但我仍然不能做到这一点。

i have been trying to pass bitmap array to another Activity for a week but i still can't do it

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延伸活动{

public class FullImageActivity extends 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类我试图意图做,但我无法通过构造函数或内部类可能发送位图阵?任何建议,请
在此先感谢**

** so i want to pass MbitArray variable to Fullactivity class i tried to do it by Intent but i can't send Bitmap array maybe via constructor or inner class ? any suggestions please thanks in advance**

推荐答案

位图实现Parcelable,你有位图的数组,你应该能够通过他们关:

Since Bitmap implements Parcelable, and you have an array of Bitmap, you should be able to pass them off:

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

然后通过<一个检索href=\"http://developer.android.com/reference/android/content/Intent.html#getParcelableArrayExtra%28java.lang.String%29\"相对=nofollow> getParcelableArrayExtra()

Intent i = getIntent();
Bitmap [] bitmaps = (Bitmap[]) i.getParcelableArrayExtra ("bitmaps");

您还可以通关的路径在一个字符串数组来 FullImageActivity ,然后就重拍他们那里,就像你在你的第一个活动一样。为了节省code,如果你选择这种方法,使得通过使静态的将是一个不错的主意共享的方法,只记得通关一个Context实例,以便 getAssets()仍可以使用:

You can also pass off the paths in a String array to FullImageActivity, then just remake them there, much like you did in your first Activity. To save code, if you choose this approach, making the method shared by making it static would be a good idea, just remember to pass off a Context instance so getAssets() can still be used:

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

然后调用,从首页

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

FullImageActivity

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

这篇关于如何通过2 activies之间的位图阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆