在android中,运用Intent跳转页面时,常用的是利用Bundle携带数据跳转到另外一个activity,其实携带图片跳转的原理也和携带数据跳转一样,首先将图片转化成bitmap,再将bitmap转化成byte数组,也就是说,根本的原理是与数据传送一样。下面是本人写的一个简单的demo,可以给大家参考参考。
在数据发送到第二个activity的时候我们还可以对图片做裁剪处理,我在下面也为大家剪贴了出来。
加载一张网络图片,并处理成byte数组,代码如下:
public class MainActivity extends AppCompatActivity {
private ImageView image;
private String path = "http://192.168.1.120/18363677172/userphoto/18363677172.png";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//严苛模式,在主线程中请求网络,不建议使用,建议新开子线程访问网络
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
.penaltyLog().penaltyDeath().build());
image = (ImageView) findViewById(R.id.imageview);
// new Thread() {
// @Override
// public void run() {
try {
image.setImageBitmap(getBitmap(path));
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "我要跳了啊", Toast.LENGTH_SHORT).show();
byte buff[] = new byte[0];//看你图有多大..自己看着改
try {
buff = Bitmap2Bytes(getBitmap(path));//这里的LZbitmap是Bitmap类的,跟第一个方法不同
} catch (IOException e) {
e.printStackTrace();
}
Intent myIntent = new Intent(MainActivity.this,Open.class);
myIntent.putExtra("bitmap",buff);
startActivity(myIntent);
finish();
}
});
} catch (Exception e) {
e.printStackTrace();
}
// }
// }.start();
}
public static Bitmap getBitmap(String path) throws IOException {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream inputStream = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
return null;
}
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
return baos.toByteArray();
}
}
接收上一个activity发送的图片byte数组,并加载图片并裁剪:
public class Open extends Activity {
private ImageView imageView;
private TextView textView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.open);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
.penaltyLog().penaltyDeath().build());
imageView= (ImageView) findViewById(R.id.image);
textView= (TextView) findViewById(R.id.tv);
Intent myIntent = getIntent();
byte buff[] = (byte[]) myIntent.getSerializableExtra("bitmap");
Bitmap bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length); // 生成位图
imageView.setImageBitmap(toRoundCorner(bitmap,2)); // 显示位图
// Bitmap bmp= BitmapFactory.decodeByteArray(buff, 0, buff.length);//重新编码出Bitmap对象
Drawable drawable =new BitmapDrawable(bmp);
// imageView.setImageBitmap(bmp);
Toast.makeText(Open.this, "byte"+buff, Toast.LENGTH_SHORT).show();
}
public static Bitmap toRoundCorner(Bitmap bitmap, float ratio) {
System.out.println("图片是否变成圆形模式了+++++++++++++");
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, bitmap.getWidth() / ratio,
bitmap.getHeight() / ratio, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
System.out.println("pixels+++++++" + String.valueOf(ratio));
return output;
}
}
最后,别忘了添加网络访问权限哦!