public class MainActivity extends AppCompatActivity {
private static int RESULT_LOAD_IMAGE = 1;
@ViewInject(R.id.iv_pic)
public ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewUtils.inject(this);
}
@OnClick(R.id.btn_selectpic)
public void selectPic(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
@OnClick(R.id.btn_camera)
protected void getImageFromCamera(View v) {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera, RESULT_LOAD_IMAGE);
} else {
Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//有时候返回的data里面没有uri而是在getExtras中
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Uri uri = data.getData();
if (uri == null) {
Bitmap bm = (Bitmap) data.getExtras().get("data");
mImageView.setImageBitmap(bm);
} else {
try {
Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
mImageView.setImageBitmap(bm);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Android 从相册选择照片 手机拍照
最新推荐文章于 2023-09-20 13:38:49 发布