本文实例为大家分享了Android实现从相册选择照片功能的具体代码,供大家参考,具体内容如下
之前在 《Android开发之裁剪照片》一文中介绍过,如何从相册选取照片和拍摄照片并进行裁切,在本篇中主要向介绍从相册选择原生照片(不裁切)。
第一步:向系统发送选择照片的意图。
private void selectFromGallery() {
// TODO Auto-generatedmethod stub
Intentintent=new Intent();
intent.setAction(Intent.ACTION_PICK);//Pick an item fromthe data
intent.setType("image
public class MainActivity extends ActionBarActivity {
private final static int SELECT_PIC=123;
private final static int SELECT_ORIGINAL_PIC=126;
private final static int TAKE_PIC=124;
private final static int TAKE_ORIGINAL_PIC=127;
private final static int CROP_PIC=125;
private Uri imageUri;
private ImageView imgShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化imageUri
imageUri=Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "test.jpg"));
imgShow=(ImageView)findViewById(R.id.imgShow);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (requestCode) {
case SELECT_PIC:
if (resultCode==RESULT_OK) {//从相册选择照片并裁切
try {
Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));//将imageUri对象的图片加载到内存
imgShow.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case SELECT_ORIGINAL_PIC:
if (resultCode==RESULT_OK) {//从相册选择照片不裁切
try {
Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex); //获取照片路径
cursor.close();
Bitmap bitmap= BitmapFactory.decodeFile(picturePath);
imgShow.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case TAKE_PIC://拍取照片,并裁切
if (resultCode==RESULT_OK) {
cropImageUri(imageUri, 600, 600, CROP_PIC);
}
case TAKE_ORIGINAL_PIC://拍取照片
if (resultCode==RESULT_OK) {
String imgPath=imageUri.getPath();//获取拍摄照片路径
}
break;
case CROP_PIC://拍取照片
if (resultCode==RESULT_OK) {
try {
Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().
openInputStream(imageUri));//将imageUri对象的图片加载到内存
imgShow.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void cropImageUri(Uri imageUri, int outputX, int outputY, int requestCode){
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image
private void selectFromGallery() {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction(Intent.ACTION_PICK);//Pick an item from the data
intent.setType("image
private void cropFromGallery() {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction(Intent.ACTION_PICK);//Pick an item from the data
intent.setType("image
private void selectFromTake() {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//将拍取的照片保存到指定URI
startActivityForResult(intent, TAKE_ORIGINAL_PIC);
}
private void cropFromTake() {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//将拍取的照片保存到指定URI
startActivityForResult(intent, TAKE_PIC);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。