public class MainActivity extends Activity {
public static final int TAKE_PHOTO=1;
public static final int CROP_PHOTO=0;
public static final int CHOOSE_PHOTO=3;
private ImageView picture;
private Button chooseFromAlbum;
private Button takePhoto;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePhoto=(Button)findViewById(R.id.take_photo);
chooseFromAlbum=(Button)findViewById(R.id.choose_from_ablum);
picture=(ImageView)findViewById(R.id.picture);
takePhoto.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
File outputImage=new File(Environment.getExternalStorageDirectory(), "output_image.jpg");
try {
if(outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
imageUri=Uri.fromFile(outputImage);
Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent,TAKE_PHOTO);
}
});
chooseFromAlbum.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent=new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, CHOOSE_PHOTO);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
if(resultCode==RESULT_OK){
Intent intent=new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image/*");
//intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CROP_PHOTO);
}
break;
case CROP_PHOTO:
if(resultCode==RESULT_OK){
try {
Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
case CHOOSE_PHOTO:
if(resultCode==RESULT_OK){
//判断手机系统版本号
if(Build.VERSION.SDK_INT>=19){//4.4及以上系统使用这个方法处理图片
handleImageOnkitKat(data);
}else{
handleImageBeforekitKat(data);//4.4及以下系统使用这个方法处理图片
}
}
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
//API 19以下
private void handleImageBeforekitKat(Intent data) {
}
//API 19
private void handleImageOnkitKat(Intent data) {
String imagePath=null;
Uri uri=data.getData();
if(DocumentsContract.isDocumentUri(MainActivity.this, uri)){
String docId=DocumentsContract.getDocumentId(uri);
if("com.android.providers.media.documents".equals(uri.getAuthority())){
String id=docId.split(":")[1];
String selection=MediaStore.Images.Media._ID+"="+id;
imagePath=getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
}else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){
Uri contentUri=ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId));
imagePath=getImagePath(contentUri,null);
}
}else if("content".equalsIgnoreCase(uri.getScheme())){
imagePath=getImagePath(uri, null);
}
displayImage(imagePath);
}
private void displayImage(String imagePath) {
if(imagePath!=null){
Bitmap bitmap=BitmapFactory.decodeFile(imagePath);
picture.setImageBitmap(bitmap);
}else{
Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show();
}
}
private String getImagePath(Uri uri, String selection) {
String path=null;
Cursor cursor=getContentResolver().query(uri, null, selection, null, null);
if(cursor!=null){
if(cursor.moveToFirst()){
path=cursor.getString(cursor.getColumnIndex(Media.DATA));
}
cursor.close();
}
return path;
}
调用摄像头和相册(调用摄像头拍照和从相册中选择图片)
最新推荐文章于 2021-05-29 05:14:01 发布