這里說(shuō)三種方式:
//1. 相冊(cè)單張照片選擇 ?(Android系統(tǒng)默認(rèn)提供了該方式)
protected void getImageFromAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");//相片類(lèi)型
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
}
//2.拍照(Android系統(tǒng)默認(rèn)提供了該方式)
protected void getImageFromCamera() {
String state = Environment.getExternalStorageState();
? ? if (state.equals(Environment.MEDIA_MOUNTED)) {
? ? Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
? ? startActivityForResult(getImageByCamera, REQUEST_CODE_CAPTURE_CAMEIA);
? ? }else {
? ? ? ? Toast.makeText(getApplicationContext(), "請(qǐng)確認(rèn)已經(jīng)插入SD卡", Toast.LENGTH_LONG).show();
? ? }
}
3.從相冊(cè)選多張圖片 需要自己查詢(xún)來(lái)實(shí)現(xiàn)
ListmDatas = new ArrayList();
// 只查詢(xún)jpeg和png的圖片
Uri ImagesUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;//獲取照片地址uri
ContentResolver contentResolver = mContext.getContentResolver();//創(chuàng)建內(nèi)容解析者
Cursor cursor = contentResolver.query(ImagesUri, null? //數(shù)據(jù)庫(kù)查詢(xún)
, MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?"
, new String[]{"image/jpeg", "image/png"}
, MediaStore.Images.Media.DATE_MODIFIED);
while (cursor.moveToNext()){
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
? ? ?mDatas.add(path);
}
cursor.close();
最好在onActivityResult()方法里得到
//? ? ? ? if(data != null){
//? ? ? ? ? ? if (requestCode == REQUEST_CODE_PICK_IMAGE) {//相冊(cè)
//? ? ? ? ? ? ? ? Uri uri = data.getData();
//? ? ? ? ? ? ? ? Log.i("TAG", "uri:--------"+uri);
//? ? ? ? ? ? ? ? byte[] bytes = getBitmapFromUri(uri);
//? ? ? ? ? ? ? ? Glide.with(mContext).load(bytes).into(ivRou);
//
//? ? ? ? ? ? } else if (requestCode == REQUEST_CODE_CAPTURE_CAMEIA ) {//相機(jī)
//
//? ? ? ? ? ? ? ? Uri uri = data.getData();
//? ? ? ? ? ? ? ? byte[] bytes = getBitmapFromUri(uri);
//? ? ? ? ? ? ? ? if(uri == null){
//? ? ? ? ? ? ? ? ? ? Bundle bundle = data.getExtras();
//? ? ? ? ? ? ? ? ? ? if(bundle != null){
//? ? ? ? ? ? ? ? ? ? ? ? bytes = (byte[]) bundle.get("data");
//? ? ? ? ? ? ? ? ? ? }
//? ? ? ? ? ? ? ? }
//? ? ? ? ? ? ? ? byte[] Bytes = getBitmapFromUri(uri);
//? ? ? ? ? ? ? ? Glide.with(mContext).load(Bytes).into(ivRou);
//? ? ? ? ? ? }
//? ? ? ? }