通过拍照或相册中获取图片,并进行裁剪操作,然后把图片显示到ImageView上。
当然也可以上传到服务器(项目中绝大部分情况是上传到服务器),参考网上资料及结合项目实际情况,
测试了多款手机暂时没有发现严重问题。代码有注释,直接贴代码:
public class UploadPicActivity extends Activity implements View.OnClickListener {
private Button take_photo_btn;
private Button select_photo_btn;
private ImageView photo_iv;
//使用照相机拍照获取图片
public static final int TAKE_PHOTO_CODE = 1;
//使用相册中的图片
public static final int SELECT_PIC_CODE = 2;
//图片裁剪
private static final int PHOTO_CROP_CODE = 3;
//定义图片的Uri
private Uri photoUri;
//图片文件路径
private String picPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_pic);
initViews();
}
private void initViews() {
this.take_photo_btn = (Button) findViewById(R.id.take_photo_btn);
this.take_photo_btn.setOnClickListener(this);
this.select_photo_btn = (Button) findViewById(R.id.select_photo_btn);
this.select_photo_btn.setOnClickListener(this);
this.photo_iv = (ImageView) findViewById(R.id.photo_iv);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
//拍照
case R.id.take_photo_btn:
picTyTakePhoto();
break;
//选择图库
case R.id.select_photo_btn:
pickPhoto();
break;
}
}
private void picTyTakePhoto() {
//判断SD卡是否存在
String SDState = Environment.getExternalStorageState();
if (SDState.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//"android.media.action.IMAGE_CAPTURE"
ContentValues values = new ContentValues();
photoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, TAKE_PHOTO_CODE);
} else {
Toast.makeText(this, "内存卡不存在", Toast.LENGTH_LONG).show();
}
}
private void pickPhoto() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image
private void startPhotoZoom(Uri uri, int REQUE_CODE_CROP) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image
private String uriToFilePath(Uri uri) {
//获取图片数据
String[] proj = {MediaStore.Images.Media.DATA};
//查询
Cursor cursor = managedQuery(uri, proj, null, null, null);
//获得用户选择的图片的索引值
int image_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//返回图片路径
return cursor.getString(image_index);
}
}
布局文件长这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/take_photo_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="拍照"
android:textSize="16sp"/>
<Button
android:id="@+id/select_photo_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="选择图片"
android:textSize="16sp"/>
<ImageView
android:id="@+id/photo_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"/>
</LinearLayout>
最后不要忘记在AndroidManifest.xml中添加UploadPicActivity及权限:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
您可能感兴趣的文章:Android 选择相册照片并返回功能的实现代码Android开发从相册中选取照片的示例代码Android通过手机拍照或从本地相册选取图片设置头像Android 实现IOS选择拍照相册底部弹出的实例android相册选择图片的编码实现代码Android7.0实现拍照和相册选取图片功能Android编程图片操作类定义与用法示例【拍照,相册选图及裁剪】Android工具类ImgUtil选择相机和系统相册Android开发实现从相册中选择照片功能详解