類似開心001的照片上傳功能,剛做的時(shí)候搜了些例子,有的好用有的不好用,最后東拼西湊,亂七八糟的寫了一個(gè)。有不對(duì)的或更好的方法希望大家指點(diǎn)。下面開始貼代碼:兩個(gè)監(jiān)聽事件:
// 拍照上傳
private OnClickListener mUploadClickListener = new OnClickListener() {
public void onClick(View v) {
// 調(diào)用相機(jī)
Intent mIntent = new Intent("android.media.action.IMAGE_CAPTURE");
// 圖片存儲(chǔ)路徑,可自定義
File tmpFile = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
// 獲取這個(gè)圖片的URI
originalUri = Uri.fromFile(tmpFile);//這是個(gè)實(shí)例變量,方便下面獲取圖片的時(shí)候用
mIntent.putExtra(MediaStore.EXTRA_OUTPUT, originalUri);
startActivityForResult(mIntent, ACTIVITY_IMAGE_CAPTURE);
}
};
// 打開相冊(cè)
private OnClickListener mPicListClickListener = new OnClickListener() {
public void onClick(View v) {
// 調(diào)用相冊(cè)
Intent mIntent= new Intent(Intent.ACTION_GET_CONTENT);
mIntent.addCategory(Intent.CATEGORY_OPENABLE);
mIntent.setType(MIME_TYPE_IMAGE_JPEG);
startActivityForResult(mIntent, ACTIVITY_GET_IMAGE);
}
};
監(jiān)聽事件寫好了,怎么調(diào)用不用我說了吧。這是個(gè)startActivityForResult事件,對(duì)應(yīng)的我們肯定得有個(gè)onActivityResult,貼之
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
Bitmap bm = null;
ContentResolver resolver = getContentResolver();
String filePath = "/sdcard/bengxin/bx_upload_tmp.jpg";//這個(gè)是暫存圖片的路徑
FileOutputStream output = null;
try {
// 創(chuàng)建暫存圖片
if (Utils.CreateFile(filePath)) {
output = new FileOutputStream(filePath);
} else {
throw new Exception("內(nèi)部錯(cuò)誤");
}
if (requestCode == ACTIVITY_GET_IMAGE) {
// 獲得圖片的uri
originalUri = data.getData();
PS:拍照的那個(gè)URI我們?cè)谏厦嬉呀?jīng)獲取了
}
/**** 獲取圖片開始 ****/
//mContent是上傳的圖片byte[]數(shù)組,得到這個(gè)后隨便怎么處理,當(dāng)然你也可以直接用fileInput流
fileInput = (FileInputStream) resolver.openInputStream(Uri
.parse(originalUri.toString()));
// 將圖片內(nèi)容解析成字節(jié)數(shù)組
mContent = getBytesFromInputStream(fileInput, 3500000);
fileInput.close();
// 將字節(jié)數(shù)組轉(zhuǎn)換為ImageView可調(diào)用的Bitmap對(duì)象
bm = getPicFromBytes(mContent, null);
/********* 獲取圖片完了 ************/
// 將圖片縮小到指定比例并且保存到緩存文件
float scale = ((float) 210) / ((float) bm.getWidth());
bm = Utils.smallBmp(bm, scale);//這個(gè)縮小功能是自己寫的方法
//將Bitmap讀到文件中去,注意這個(gè)是壓縮,那個(gè)100是壓縮比,0-100,越大質(zhì)量越好
bm.compress(CompressFormat.JPEG, 100, output);
output.flush();
output.close();
/*********為了更快速的將圖片上傳,將縮小后的圖片保存到暫存文件***************/
fileInput = new FileInputStream(filePath);
// 將圖片內(nèi)容解析成字節(jié)數(shù)組
mContent = getBytesFromInputStream(fileInput, 3500000);
fileInput.close();
/*********************/
// 預(yù)覽一下你的圖片吧
bm = bm.createScaledBitmap(bm, mButtomUpload.getWidth() - 10,
mButtomUpload.getHeight() - 10, true);
mButtomUpload.setImageBitmap(bm);
mButtomUpload.setPadding(2, 2, 2, 2);
} catch (Exception e) {
Utils.exceptionShow(CheckIn.this, e.getMessage());
}
}
附贈(zèng)兩個(gè)方法,一個(gè)將字節(jié)轉(zhuǎn)換成bitmap,一個(gè)獲取byte[]數(shù)組
private static Bitmap getPicFromBytes(byte[] bytes,
BitmapFactory.Options opts) {
if (bytes != null)
if (opts != null)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
opts);
else
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return null;
}
private static byte[] getBytesFromInputStream(InputStream is, int bufsiz)
throws IOException {
int total = 0;
byte[] bytes = new byte[4096];
ByteBuffer bb = ByteBuffer.allocate(bufsiz);
while (true) {
int read = is.read(bytes);
if (read == -1)
break;
bb.put(bytes, 0, read);
total += read;
}
byte[] content = new byte[total];
bb.flip();
bb.get(content, 0, total);
return content;
}
好了,試一下吧
|