无法在Android活动之间传递位图

3

我需要动态地更改活动的背景,通过从其他活动中选择图像并将所选图像传回调用活动,但是当我尝试获取图像时,它为null。以下是我的代码:

  public class SelectGoalBackground extends OrmLiteBaseActivity<DatabaseHelper> {// implements{



GridView gridview ;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.select_goal_background);
   final String from = getIntent().getExtras().getString("from");
    goalsList = new ArrayList<Goal>();

    try {
        goalsList = getTop3Goals();
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (java.sql.SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


     gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

           ImageView l = (ImageView)gridview.getChildAt(position);
              Drawable path= l.getDrawable();


            Bitmap bitmap = ((BitmapDrawable)path).getBitmap();

            Intent intent = new Intent(SelectGoalBackground.this, AlarmAppActivity.class);

            intent.putExtra("Bitmap", bitmap);
            startActivity(intent);
            SelectGoalBackground.this.finish();
            return;

    });

}


public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }
       Bitmap bm = BitmapFactory.decodeResource(getResources(), mThumbIds[position]);
        imageView.setImageBitmap(bm);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_0, R.drawable.sample_1

    };
}

在调用的活动中,我可以像这样获取位图

    RelativeLayout rel_lay = (RelativeLayout) findViewById(R.id.main_lay);
    Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");


    if (bitmap != null) {

        Log.v("bitmap", "not null");
        Drawable drawable = new BitmapDrawable(getResources(), bitmap);
        rel_lay.setBackgroundDrawable(drawable);
    }

但是这里的位图为空,而且它没有设置这个活动的背景。任何形式的帮助都将不胜感激,请帮帮忙。
1个回答

9

有三种解决此问题的方法。

1)首先将图像转换为字节数组,然后将其传递到Intent中,在下一个活动中从Bundle获取字节数组并转换为图像(Bitmap),然后设置到ImageView中。

将Bitmap转换为字节数组:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

将字节数组传递到意图中:

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

从Bundle获取Byte数组并转换为位图图像:
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

2) 首先将图像保存到SD卡中,然后在下一个活动中将此图像设置到ImageView中。

3) 将Bitmap传递到Intent中,并从bundle中获取下一个活动中的bitmap,但问题是,如果您的Bitmap / Image大小很大,则该图像在下一个活动中不会加载。


非常感谢您,Dipak。我知道原因了,我的图像尺寸太大了,所以传递字节数组和位图都不起作用。我想将图像保存到SD卡上可能是解决方案,或者我应该调整图像大小?有什么建议吗?谢谢。 - MobileDev Fast
@MobileDevFast 是的,您可以使用bitmap.createscaledbitmap调整图片大小。 - Dipak Keshariya
为什么我在第二个活动中使用 putExtra("picture", byteArray); 传递字节数组时得到了空数组? - KasparTr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接