在android中,运用Intent跳转页面时,常用的是利用Bundle携带数据跳转到另外一个activity,其实携带图片跳转的原理也和携带数据跳转一样,首先将图片转化成bitmap,再将bitmap转化成byte数组,也就是说,根本的原理是与数据传送一样。下面是本人写的一个简单的demo,可以给大家参考参考。
在数据发送到第二个activity的时候我们还可以对图片做裁剪处理,我再下面也为大家剪贴了出来。
一、第一个activity
public class MainActivity extends AppCompatActivity {
private ImageView image;
@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);
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));
} 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();
}
}
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
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));
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;
}
}
最后,别忘了在mainfest中添加访问网络权限哦!!!