Android 如何在两个活动之间传递位图数组
一周来,我一直在尝试将位图数组传递给另一个活动,但仍然无法完成Android 如何在两个活动之间传递位图数组,android,android-intent,bitmap,Android,Android Intent,Bitmap,一周来,我一直在尝试将位图数组传递给另一个活动,但仍然无法完成 public class HomePage extends Activity { private GridView gridView; public Bitmap[] mBitArray; public Bitmap[] mBitArray5; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceS
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;
}
所以我想将MbitArray变量传递给Fullactivity类。我是有意这样做的,但我无法通过构造函数或内部类发送位图数组?由于实现了可包裹,并且您有一个位图数组,您应该能够传递它们:
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
i.putExtra("bitmaps", mBitArray);
startActivity(i);
然后通过
您还可以将字符串数组中的路径传递到FullImageActivity
,然后在那里重新创建它们,就像您在第一个活动中所做的那样。要保存代码,如果您选择这种方法,通过使其成为静态来共享该方法将是一个好主意,请记住传递上下文实例,这样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;
}
然后从主页
mBitArray[0] = getBitmapFromAsset(this,"Gallary/g1p1.jpg");
以及来自FullImageActivity
otherBitmapArray[0] = HomePage.getBitmapFromAsset(this, bitmapPaths[0]);
如果你只是在阅读你的资源,我不明白你为什么要这么做。只需重新阅读,而不是为活动添加额外的权重。我确实这样做了。我在FullImageActivity中重新定义了数组,但在查看3~4张图片后出现内存不足错误。你是否回收了所有位图?不,我没有搜索有关回收的信息,我在某个地方读过“仅当不再需要位图时,才应回收位图。但是,当您想要显示位图时,确实需要位图。如果不想显示位图,则可以循环使用位图。“所以我应该回收我的位图数组,不包括正在显示的位图?谢谢,只是好奇。如果我们传递一个大的位图,性能会受到影响吗?@system32就像任何正在传输的东西一样,大小越大,性能受到的影响就越大。然而,Parcelable比Serializable效率更高,所以从某种意义上说这仍然是“更好的”。但是它如何从路径中重新读取,这取决于设备和位图大小。@JRowan记住,如果您放置模型对象、位图,或
Bundle
@JRowan中的基元类型保持静态意味着它属于dalvik实例-如果它们都访问相同的静态数组,则A
中的修改将反映在B
中。制作包裹不会保留引用(这仍然是“序列化”的一种方式),并且每个数组都是活动的本地数组。@system32 Serializable可以从活动/服务传递到活动/服务。
mBitArray[0] = getBitmapFromAsset(this,"Gallary/g1p1.jpg");
otherBitmapArray[0] = HomePage.getBitmapFromAsset(this, bitmapPaths[0]);