編輯:關於android開發
在android中,運用Intent跳轉頁面時,常用的是利用Bundle攜帶數據跳轉到另外一個activity,其實攜帶圖片跳轉的原理也和攜帶數據跳轉一樣,首先將圖片轉化成bitmap,再將bitmap轉化成
byte
數組,也就是說,根本的原理是與數據傳送一樣。下面是本人寫的一個簡單的demo,可以給大家參考參考。
在數據發送到第二個activity的時候我們還可以對圖片做裁剪處理,我再下面也為大家剪貼了出來。
一、第一個activity
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
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;
}
}
最後,別忘了在mainfest中添加訪問網絡權限哦!!!
Android筆記——Android中數據的存儲方式(三),android筆記 Android系統集成了一個輕量級的數據庫:SQLite,所以Android對數據庫的支
Android 博客園客戶端 (五) 查看評論、搜索博主,android查看評論項目地址:https://github.com/ZhangTingkuo/AndroidC
Android自定義控件5--輪播圖廣告ViewPager基本實現,androidviewpager本文地址:http://www.cnblogs.com/wuyudon
Android開發技巧之Camera拍照功能 本篇是我對開發項目的拍照功能過程中,對Camera拍照使用的總結。由於camera2是在api level 21(5.0