android相冊獲取照片

這里說三種方式:


//1. 相冊單張照片選擇 ?(Android系統默認提供了該方式)

protected void getImageFromAlbum() {

Intent intent = new Intent(Intent.ACTION_PICK);

intent.setType("image/*");//相片類型

startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);

}


//2.拍照(Android系統默認提供了該方式)

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(), "請確認已經插入SD卡", Toast.LENGTH_LONG).show();

? ? }

}


3.從相冊選多張圖片 需要自己查詢來實現

ListmDatas = new ArrayList();

// 只查詢jpeg和png的圖片

Uri ImagesUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;//獲取照片地址uri

ContentResolver contentResolver = mContext.getContentResolver();//創建內容解析者

Cursor cursor = contentResolver.query(ImagesUri, null? //數據庫查詢

, 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) {//相冊

//? ? ? ? ? ? ? ? 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 ) {//相機

//

//? ? ? ? ? ? ? ? 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);

//? ? ? ? ? ? }

//? ? ? ? }

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容