Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

android - how to pass images through intent?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...