android - 如何通过 Intent 传递图像?

标签 android android-intent

头等舱

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

二等舱
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"));
  } 
}

通过使用它,我可以很好地显示文本,但我无法在第二个 Aactivity 上显示图像。

最佳答案

您可以将其作为字节数组传递并构建位图以在下一个屏幕上显示。但是使用 Intent 我们可以发送小图像,如缩略图、图标等。否则它会导致绑定(bind)失败。

发件人 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);

接收器 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);
}

关于android - 如何通过 Intent 传递图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23577827/

相关文章:

android - 有没有一种简单的方法可以在 Ionic2 中存储密码?

android - 无法解析目标 'android-14'

android - 如何在谷歌地图上找到两点之间的最佳路线然后突出显示它?

android - getExtras(name).getString 和 getIntent().getStringExtra(name) 的区别

android - 如何让我的 Android 应用程序出现在另一个特定应用程序的共享列表中

java - Socket.io 发出但不触发监听器

android - 清除除 android 中的一个以外的所有 Activity

播放视频的Android Intent ?

android - GCM Intent.getExtras——如何仅显示特定数据?

android - 在 Gingerbread 上安装本地 .apk 文件的 Intent