Android实现拍照或者选取本地图片
短信预约 -IT技能 免费直播动态提醒
本文实例为大家分享了Android实现拍照或者选取本地图片的具体代码,供大家参考,具体内容如下
总体流程
从selectPhotoActivity中启动图册或者相机,再根据获取的uri进行裁剪,返回uri,再对这个uri执行一系列操纵。
从相册选取图片
private void pickPhoto() {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
}
使用隐式intent启动相机。
拍照获取图片
private void takePhoto() {
// 执行拍照前,应该先判断SD卡是否存在
String SDState = Environment.getExternalStorageState();
if (SDState.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"
File path = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File file = new File(path, IMAGE_FILE_NAME);
takePhoto = Uri.fromFile(file);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, takePhoto);
startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
} else {
Toast.makeText(getApplicationContext(), "内存卡不存在",
Toast.LENGTH_SHORT).show();
}
}
值得注意的一点是,intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, takePhoto)中,设置了拍完照照片的存放路径takePhoto,在此情况下,部分机型的onActivityResult()中不会返回数据,即data.getData()为空,因为可以根据存放路径即可获取拍照图片。
裁剪图片
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = { column };
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
整体代码
public class selectPhotoActivity extends Activity implements View.OnClickListener{
public static final int SELECT_PIC_BY_TACK_PHOTO = 1;
public static final int SELECT_PIC_BY_PICK_PHOTO = 2;
private static final int RESULT_REQUEST_CODE = 3;
private Button btn_take_photo;
private Button btn_pick_photo;
private Button btn_cancel;
private static final String IMAGE_FILE_NAME = "image.jpg";
private String picPath;
//保存裁剪后的图像
private Bitmap photo;
private Intent lastIntent;
private Uri takePhoto;
public static final String KEY_PHOTO_PATH = "photo_path";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_photo);
btn_take_photo = (Button) findViewById(R.id.btn_take_photo);
btn_pick_photo = (Button) findViewById(R.id.btn_pick_photo);
btn_cancel = (Button) findViewById(R.id.btn_cancel);
lastIntent = getIntent();
btn_take_photo.setOnClickListener(this);
btn_pick_photo.setOnClickListener(this);
btn_cancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_take_photo : // 开启相机
takePhoto();
break;
case R.id.btn_pick_photo : // 开启图册
pickPhoto();
break;
case R.id.btn_cancel : // 取消操作
this.finish();
break;
default :
break;
}
}
private void takePhoto() {
// 执行拍照前,应该先判断SD卡是否存在
String SDState = Environment.getExternalStorageState();
if (SDState.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"
File path = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File file = new File(path, IMAGE_FILE_NAME);
takePhoto = Uri.fromFile(file);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, takePhoto);
startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
} else {
Toast.makeText(getApplicationContext(), "内存卡不存在",
Toast.LENGTH_SHORT).show();
}
}
private void pickPhoto() {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
finish();
return super.onTouchEvent(event);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
Log.e("TAG","ActivityResult resultCode error");
return;
}
switch (requestCode){
case SELECT_PIC_BY_PICK_PHOTO:
Uri uri = data.getData();
if (!TextUtils.isEmpty(uri.getAuthority())) {
//查询选择图片
Cursor cursor = getContentResolver().query(
uri,
new String[] { MediaStore.Images.Media.DATA },
null,
null,
null);
//返回 没找到选择图片
if (null == cursor) {
return;
}
//光标移动至开头 获取图片路径
cursor.moveToFirst();
picPath = cursor.getString(cursor
.getColumnIndex(MediaStore.Images.Media.DATA));
Log.d("图片路径啊啊啊啊啊啊",picPath);
}
break;
case SELECT_PIC_BY_TACK_PHOTO:
//裁剪图片
startPhotoZoom(takePhoto);
break;
case RESULT_REQUEST_CODE :
if (data != null) {
Log.d("图片路径",data.getData().toString());
picPath = getPathByUri4kitkat(getApplicationContext(),data.getData());
Log.d("图片路径啊啊啊啊啊啊",picPath);
}
break;
}
if(requestCode != SELECT_PIC_BY_TACK_PHOTO) {
lastIntent.putExtra(KEY_PHOTO_PATH, picPath);
setResult(Activity.RESULT_OK, lastIntent);
finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = { column };
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341