Android 调用系统相册(未进行权限判断)
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
// intent.setType( "image/*");(此种方式也可)
startActivityForResult(intent,ALBUM); //ALBUM 表示声明的requestCode
一个根据uri获取图片的地址
private String getPathByUri(Context context,Uri uri){
String path =null;
String scheme = uri.getScheme();
if(scheme==null){
path = uri.getPath();
}else if(ContentResolver.SCHEME_FILE.equals(scheme) ){
path = uri.getPath();
}else if(ContentResolver.SCHEME_CONTENT.equals(scheme)){
Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA},null,null,null);
if(null!= cursor){
if(cursor.moveToFirst()){
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
if(index > -1){
path = cursor.getString(index);
}
}
}
cursor.close();
}
return path;
}
在onActivityResult 中获取返回的结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data!=null){
if(RESULT_OK == resultCode &&resultCode== ALBUM ){
Uri uri = data.getData();
if(uri!=null){
setImg(uri);
}
}
}
}
将图片显示在一个imagevie上
private void setImg(Uri uri){
String imgPath= getPathByUri(PhotoActivity.this,uri);
Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
mImageView.setImageBitmap(bitmap);
}
接下来是获取系统相机,一种是只获取拍照后的缩略图一种是直接返回拍照后的图片地址
1.获取缩略图的方式:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,TAKE_PICTURE_THUMB);//TAKE_PICTURE_THUMB requestCode
在onActivityResult中的主要代码
if(data.hasExtra("data")){
Bitmap thumbnail = data.getParcelableExtra("data");
mImageView.setImageBitmap(thumbnail);
}
2.获取图片的路径,在Android7.0中以往的获取方式会出现异常,需要使用FileProvider来实现兼容的拍照
官网问题说明
FileProvider用法
具体实现:
<1> 在manifest添加一个provider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.vc.xxx.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path" />
provider>
<2>在res的目录添加一个file_path.xml 如下
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<root-path name="root" path="" />
<files-path name="files" path="" />
<cache-path name="cache" path="" />
<external-path name="external" path="vcvc" />
<external-files-path name="name" path="path" />
<external-cache-path name="name" path="path" />
paths>
以上完成后接下来看调用的具体方法执行之前需要判断
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) )后在执行拍照
// mImgPath图片地址的变量
private void takePhotos(){
Uri mUri =null;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/vcvc/");
if(!file.exists()){
file.mkdirs();
}
mImgPath = Environment.getExternalStorageDirectory().getPath()+"/vcvc/" +System.currentTimeMillis() +".png";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
mUri = FileProvider.getUriForFile(PhotoActivity.this,"com.vc.xxx.fileprovider",new File(mImgPath)); //7.0需要兼容方式
}else {
mUri = Uri.fromFile(new File(mImgPath));
}
intent.putExtra(MediaStore.EXTRA_OUTPUT,mUri);
startActivityForResult(intent,TAKE_PICTURE);
}
至此可以在onActivityResult 获取返回的结果集,当然对于一些手机拍照比较大的还是最好对图片进行一个压缩
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = BitmapFactory.decodeFile(mImgPath);
mImageView.setImageBitmap(bitmap);
}
在其他机型还有问题的话欢迎拍砖
参考
鸿神的这篇
还有吴小龙童鞋