能不能通过在Android的活动之间的位图 [英] Can not pass bitmap between activities in android

查看:122
本文介绍了能不能通过在Android的活动之间的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动态改变活动的背景,通过从其他活动中选择一个图像。并通过所选择的图像返回给调用活动,但是当我尝试获取图像,它为空。 这里是我的code。

I need to change background of the activity dynamically , by selecting an image from an other activity. and passing the selected image back to the calling activity , but when i try to get the image , it is null. here is my code.

  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

    };
}

和呼吁活动,我得到这个位图这样的

and in calling activity i get this bitmap like this

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

但位图是空这里,​​它不设置此活动的背景。 任何形式的帮助是AP preciated,plz帮助

but bitmap is null here and it is not setting background of this activity. any kind of help is appreciated , plz help

推荐答案

有3解决方案来解决这个问题。

There are 3 Solutions to solve this issue.

1)首先将图像转换成字节数组,然后传递到意向并在接下来的活动得到捆绑字节数组转换为图像(位图),并设置成ImageView的。

1) First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView.

位图转换为字节数组: -

Convert Bitmap to Byte Array:-

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

通字节数组的意图: -

Pass byte array into intent:-

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

获取字节数组的捆绑,并转换为位图图像: -

Get Byte Array from Bundle and Convert into Bitmap Image:-

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的。

2) First Save image into SDCard and in next activity set this image into ImageView.

3)合格的位图到意向并得到位从包下一个活动,但问题是,如果你的位图/影像尺寸大当时的形象是不是在明年的活动加载。

3) Pass Bitmap into Intent and get bitmap in next activity from bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity.

这篇关于能不能通过在Android的活动之间的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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