0

I am making an application in Android, which requires sending bitmap object from one activity and displaying that bitmap object sent on the second activity page. But , I am getting a blank screen

Here is a sample of my code for sending the Bitmap object :-

Intent intent = new Intent(Display2.this, Display3.class);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.RGB_565);
intent.putExtra("BitmapImage", bitmap);
startActivity(intent);

Now the part of code on second activity to retrieve the sent object and displaying it on screen :-

public class Display3 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("BitmapImage");
        ImageView myIV = (ImageView) findViewById(R.id.imageView1);
        bitmap = bitmap.createBitmap(myIV.getWidth(), myIV.getHeight(), Bitmap.Config.RGB_565);
        myIV.setImageBitmap(bitmap);
        setContentView(R.layout.display3);

    }
}

can anyone suggest whats wrong in this part ?

Thanks !

Prashant Singh
  • 3,725
  • 12
  • 62
  • 106

5 Answers5

2

Bitmap implements Parcelable object, so you could always pass it in the intent like below :

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

and retrieve it on the other end:

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
Maulik J
  • 2,745
  • 19
  • 22
2

this is not good for passing bitmap from one activity to another..

You can simply name you Bitmap as static first. then create a method like

public static Bitmap getBitmap(){
    return bitmap;
}

then you can simply call from other activities,

bitmapexistingclass.getBitmap();

if we use intents for passing bitmap we will get some errors check this question How to pass bitmap from one activity to another

Community
  • 1
  • 1
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
1

from your code above you are creating an empty bitmap:

ImageView iv = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.RGB_565);

I do not see any reason why from this code you'll get anything else then blank black bitmap with hight and width equals to the imageView hight and width

Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
0

what you are expecting from android to fill drawing in Bitmap itself , you are creating an Empty Bitmap Object with height and width equavalent to ImageView object; as Bitmap implements Parcelable you can simply put it in bundle and retrive on other Activity

Yahya Arshad
  • 1,626
  • 2
  • 19
  • 34
0

Use below two methods to convert a bitmap to string and vice versa. Then you can pass the string with intents from activity to another activity. I too do the same in my application. Then we dont need to use ParcelableExtra and serialzable class.

public String convertBitmapToString(Bitmap src) {
if(src!= null){
ByteArrayOutputStream os=new ByteArrayOutputStream();
src.compress(android.graphics.Bitmap.CompressFormat.PNG, 100,(OutputStream) os);
byte[] byteArray = os.toByteArray();
return Base64.encodeToString(byteArray,Base64.DEFAULT);  
}
return null;            
}

public Bitmap getBitMapFromString(String src){
Bitmap bitmap = null;
if(src!= null){
byte[] decodedString = Base64.decode(src.getBytes(), Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
return bitmap;
}
return null;
}
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64