3

First class

public class ChooseDriver extends Activity implements OnItemClickListener {

  private static final String rssFeed   = "http://trade2rise.com/project/seattle/windex.php?itfpage=driver_list";  
  private static final String ARRAY_NAME = "itfdata";
  private static final String ID = "id";
  private static final String NAME = "name";
  private static final String IMAGE = "image";

  List<Item> arrayOfList;
  ListView listView;
  MyAdapter objAdapter1;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.choose_driver);

    listView = (ListView) findViewById(R.id.listView1);
    listView.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        Item item = (Item) objAdapter1.getItem(position);
        Intent intent = new Intent(ChooseDriver.this, DriverDetail.class);
        intent.putExtra(ID, item.getId());
        intent.putExtra(NAME, item.getName().toString());
        // intent.putExtra(IMAGE, item.getImage().toString());        
        // image.buildDrawingCache();
        // Bitmap image= image.getDrawingCache();
        Bundle extras = new Bundle();
        // extras.putParcelable("imagebitmap", image);
        intent.putExtras(extras);

        startActivity(intent); 
      }
    });
  }
}

Second class

public class DriverDetail extends Activity {

  private ImageLoader imageLoader;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.driver_detail);

    ImageView imageView = (ImageView) findViewById(R.id.imageView1);
    TextView tv = (TextView) findViewById(R.id.tvDname);

    Bundle extras = getIntent().getExtras();
    Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
    imageView.setImageBitmap(bmp );
    //imageView.set(getIntent().getExtras().getString("image"));
    tv.setText(getIntent().getExtras().getString("name"));
  } 
}

By using it I can show text very well, but I can't show an image on second Aactivity.

Sky Kelsey
  • 19,010
  • 5
  • 35
  • 74
  • 1
    i think you have just pass image path as string and try to load image from this path on other activity. – Haresh Chhelana May 10 '14 at 06:32
  • you never pass image you just play with their path – raj May 10 '14 at 06:37
  • Have you used this extras.putParcelable("imagebitmap", image);? – Piyush May 10 '14 at 06:38
  • how to pass image through the path?I am a new user.please help me.. –  May 10 '14 at 06:39
  • possible duplicate of [how do you pass images (bitmaps) between android activities using bundles?](http://stackoverflow.com/questions/4352172/how-do-you-pass-images-bitmaps-between-android-activities-using-bundles) – dcanh121 May 10 '14 at 06:52
  • Check this out http://stackoverflow.com/q/4352172 and http://stackoverflow.com/q/12908048 and http://stackoverflow.com/a/11720640 – dcanh121 May 10 '14 at 06:53

1 Answers1

11

You can pass it as a byte array and build the bitmap for display on the next screen. but using intent we can send small images like thumbnails,icons etc.else it will gives bind failure.

Sender Activity

Intent _intent = new Intent(this, newscreen.class);
Bitmap _bitmap; // your bitmap
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
_bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
_intent.putExtra("byteArray", _bs.toByteArray());
startActivity(_intent);

Receiver Activity

if(getIntent().hasExtra("byteArray")) {
ImageView _imv= new ImageView(this);
Bitmap _bitmap = BitmapFactory.decodeByteArray(
        getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);        
_imv.setImageBitmap(_bitmap);
}
GIRIDHARAN
  • 169
  • 1
  • 1
  • 14
Pankaj Arora
  • 11,041
  • 2
  • 38
  • 62
  • 1
    While this would work, but this will be a huge overkill. One could pass a path and get it done in a much lighter way – Aman Alam Feb 05 '15 at 12:43
  • Hi. i got this error java.lang.OutOfMemoryError. while Receiver Activity is getting the image. what will i do. i read this but i cant understand http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap – Vrajesh May 11 '15 at 09:39
  • @AmanAlam path can be passed simply by intent.putextra ("mediaPath", "path_value"), as path is a string. – Pankaj Arora Aug 06 '20 at 14:44
  • 1
    @PankajArora Yes of course, and that would be a much more efficient way of doing things than passing the bitmap object – Aman Alam Sep 09 '20 at 04:47