Unable to transfer bitmap between actions in android

I need to dynamically change the background of activity, selecting an image from another action. 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 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 when the activity is called, 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); } 

but the bitmap is null here, and it does not set the background of this activity. any help is appreciated, plz help

+4
source share
1 answer

There are 3 solutions to this problem.

1) First convert the image to a byte array, and then go to Intent and in the next step get the byte array from Bundle and Convert into Image (Bitmap) and set it to 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 an array of bytes to the target: -

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

Get byte array from Bundle and convert to bitmap: -

 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) First save the image in the SDCard and in the next step set this image to ImageView.

3) Pass Bitmap to Intent and get a bitmap in the next activity from the package, but the problem is that the size of your Bitmap / Image is large at the time when the image does not load in the next step.

+10
source

All Articles