(一)从本地相册选择和拍照
1、调用本地相册:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.putExtra("crop", "true");//选择图片后进行裁剪
intent.putExtra("aspectX", 1);//裁剪的宽高比
intent.putExtra("aspectY", 1);//裁剪的宽高比
intent.putExtra("outputX", 180);//输出的图片宽
intent.putExtra("outputY", 180);//输出的图片高
intent.putExtra("return-data", true);
mContext.startActivityForResult(intent, ALBUM);
//ALBUM为activity的onActivityResult中的处理标志
2、调用拍照:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "camera.jpg")));
//拍出来的照片放在主目录下,名为camera.jpg
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
mContext.startActivityForResult(intent, CAMERA);
//CAMERA为activity的onActivityResult中的处理标志
2.2若是拍照后的图片有裁剪要求,则在onActivityResult中再发出裁剪指令
3、调用系统裁剪:
private void cropImageUri(Uri uri, int outputX, int outputY) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.putExtra("return-data", false);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, CameraDialog.CROP);
}
4、onActivityResult的响应:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case CameraDialog.CAMERA:
//发出裁剪指令(指定文件路径、裁剪的输入宽高)
cropImageUri(Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "camera.jpg")), 180, 180);
return;
case CameraDialog.ALBUM:
bitmap = (Bitmap) data.getExtras().get("data");
break;
case CameraDialog.CROP:
bitmap = BitmapUtil.decodeUriAsBitmap(mActivity, Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "camera.jpg")));
break;
}
if (bitmap == null) {
Toast.makeText(mActivity, mActivity.getString(R.string.error_pickimg), Toast.LENGTH_SHORT).show();
return;
}
//将获取到的bitmap进行上传服务器等业务处理
}
super.onActivityResult(requestCode, resultCode, data);
}
实践基本如下(附上上述的onActivityResult和裁剪函数即可):
public class CameraDialog extends AlertDialog {
BaseActivity mContext;
public static final int CAMERA = 10;
public static final int ALBUM = 11;
public static final int CROP = 12;
public CameraDialog(BaseActivity context) {
super(context);
mContext = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
applyCompat();
setContentView(R.layout.dialog_camera);
TextView tip1 = (TextView) findViewById(R.id.camera);
TextView tip2 = (TextView) findViewById(R.id.album);
tip1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//打开Camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "camera.jpg")));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
mContext.startActivityForResult(intent, CAMERA);
dismiss();
}
});
tip2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//打开相册(Intent.ACTION_GET_CONTENT存在机型获取不到数据的情况,改用Intent.ACTION_PICK改善)
// Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 180);
intent.putExtra("outputY", 180);
intent.putExtra("return-data", true);
mContext.startActivityForResult(intent, ALBUM);
dismiss();
}
});
getWindow().setBackgroundDrawable(new ColorDrawable());
}
private void applyCompat() {
if (Build.VERSION.SDK_INT < 19) {
return;
}
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
(二)打开本地文件选择器
1、调用本地文件选择器
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");//设置类型,我这里是任意类型,任意后缀的可以这样写。调用指定文件类型的文件选择器,参考下文“另外”
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent,1);//1为标志,可在onActivityResult进行回调区分
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {//是否选择,没选择就不会继续
Uri uri = data.getData();//得到uri,后面就是将uri转化成file的过程。
//避免获取path时,Cursor为null的解决方案:scheme进行判断
String scheme = uri.getScheme();
String path = null;
if (scheme == null)
path = uri.getPath();
else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
path = uri.getPath();
} else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor actualimagecursor = getContentResolver().query(uri, proj, null, null, null);
if (actualimagecursor != null && actualimagecursor.moveToFirst()) {
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
path = actualimagecursor.getString(actual_image_column_index);
}
actualimagecursor.close();
}
if (path != null)
Toast.makeText(PickActivity.this, path, Toast.LENGTH_SHORT).show();
}
}
发出调用请求后,效果如下:
2、另外
实践效果:
通过intent的type设置不同,发出不同的响应
intent.setType(“image/*”);
//intent.setType(“audio/*”); //选择音频
//intent.setType(“video/*”); //选择视频 (mp4 3gp 是android支持的视频格式)
//intent.setType(“video/*;image/*”);//同时选择视频和图片
图片、视频、同时选择视频图片:
音频:(直接打开了文件管理器)
(以上实践均为坚果手机效果)
参考:
1.《android图片文件的路径地址与Uri的相互转换》
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0821/1660.html
2.Android调用系统自带的文件管理器进行文件选择并获得路径,android文件管理器
http://www.bkjia.com/Androidjc/1075240.html