栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android编程使用Intent传递图片的方法详解

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android编程使用Intent传递图片的方法详解

本文实例讲述了Android编程使用Intent传递图片的方法。分享给大家供大家参考,具体如下:

基本思路是先把bitmap转化为byte数组,用Intent传递数组,在将数组转化为bitmap

bitmap转化为byte数组的方法:

private byte[] Bitmap2Bytes(Bitmap bm){
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
  return baos.toByteArray();
}

byte数组转化为bitmap方法:

byte buff[]=mIntent.getByteArrayExtra("image");
bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);

程序实例:

第一个activity:

import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class SendImageActivity extends Activity implements onClickListener {
  
  private Bitmap bitmap;
  byte buff[] = new byte[125*250];
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView mImageView = (ImageView) findViewById(R.id.image);
    Button bt1 = (Button) findViewById(R.id.bt1);
    bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.option24);
    buff = Bitmap2Bytes(bitmap);
    BitmapDrawable mBitmapDrawable = new BitmapDrawable(bitmap);
    mImageView.setBackgroundDrawable(mBitmapDrawable);
    bt1.setonClickListener(this);
  }
  @Override
  public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent mIntent = new Intent();
    mIntent.putExtra("image", buff);
    mIntent.setClass(this, activity2.class);
    startActivity(mIntent);
  }
  private byte[] Bitmap2Bytes(Bitmap bm){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
    }
}

第二个activity:

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class activity2 extends Activity {
  private Bitmap bitmap;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout2);
    ImageView mImageView = (ImageView) findViewById(R.id.image2);
    Intent mIntent = getIntent();
    byte buff[]=mIntent.getByteArrayExtra("image");
    bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);
    BitmapDrawable mBitmapDrawable = new BitmapDrawable(bitmap);
    mImageView.setBackgroundDrawable(mBitmapDrawable);
  }
}

发送图片:

Intent intent = new Intent(ChangePortraitActivity.this , UserProfileActivity.class);
mImageView.setDrawingCacheEnabled(Boolean.TRUE);
intent.putExtra("BITMAP", mImageView.getDrawingCache()); //这里可以放一个bitmap
startActivity(intent);

接收图片:

//接收的activity
Intent intent = getIntent();
if (intent != null && intent.getParcelableExtra("BITMAP") != null) {
  Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("BITMAP");
  mImageViewPortrait.setImageBitmap(bitmap);
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/159620.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号