调用系统相机拍照获取图片或者从相册中调用本地图片显示问题
2016-12-08 16:08
771 查看
需求是这样的:打开系统或者第三方相机app拍照,然后使用所拍照片进行处理,或者直接调用系统或者第三方相册app,选择图片返回使用。需求很简单,过程很蛋疼,网上有很多方法,不过有很多不好用的地方,最后使用的这种方式还不错,记录一下,还是那句话,我很懒,以后复制使用直接复制代码:
1.调用相机拍照返回所拍照片(不是缩略图),点击按钮,调用相机:
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(getExternalFilesDir(Environment.DIRECTORY_DCIM) +"/bidata");
if (!file.exists()) {
file.mkdirs();
}
intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(file, "cameraTmp.png")));
startActivityForResult(intent, 0); 获取所得照片:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 0:
Bitmap bitmap = CommonUtils.getBitmap(this, getExternalFilesDir(Environment.DIRECTORY_DCIM) + "/bidata/"+ "cameraTmp.png");
break;
}
super.onActivityResult(requestCode, resultCode, data);
} 2.调用相册获取图片
Intent picture = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(picture, 1); 获取所得到的图片:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 1:
CommonUtils.bitmap = BitmapFactory.decodeFile(CommonUtils.getRealFilePath(this, data.getData()));
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
} 最后把两个加载bitmap的方法贴出来:
/**
* 由string类型的路径加载图片
*
* @param context
* @param path
* @return
*/
public static Bitmap getBitmap(Context context, String path) {
Uri uri = Uri.fromFile(new File(path));
InputStream in = null;
try {
in = context.getContentResolver().openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
if (o.outHeight > 1024 || o.outWidth > 1024) {
scale = (int) Math.pow(
2,
(int) Math.round(Math.log(1024 / (double) Math.max(
o.outHeight, o.outWidth)) / Math.log(0.5)));
}
if (scale < 4) {
scale = 4;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
in = context.getContentResolver().openInputStream(uri);
Bitmap b = BitmapFactory.decodeStream(in, null, o2);
in.close();
return b;
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
return null;
}
/**
* 将图片的uri转为绝对路径
*
* @param context
* @param uri
* @return
*/
public static String getRealFilePath(final Context context, final Uri uri) {
if (null == uri)
return null;
final String scheme = uri.getScheme();
String data = null;
if (scheme == null)
data = uri.getPath();
else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
data = 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) {
data = cursor.getString(index);
}
}
cursor.close();
}
}
return data;
} 也 希望对有需要的战友有帮助。。。。
1.调用相机拍照返回所拍照片(不是缩略图),点击按钮,调用相机:
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(getExternalFilesDir(Environment.DIRECTORY_DCIM) +"/bidata");
if (!file.exists()) {
file.mkdirs();
}
intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(file, "cameraTmp.png")));
startActivityForResult(intent, 0); 获取所得照片:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 0:
Bitmap bitmap = CommonUtils.getBitmap(this, getExternalFilesDir(Environment.DIRECTORY_DCIM) + "/bidata/"+ "cameraTmp.png");
break;
}
super.onActivityResult(requestCode, resultCode, data);
} 2.调用相册获取图片
Intent picture = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(picture, 1); 获取所得到的图片:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 1:
CommonUtils.bitmap = BitmapFactory.decodeFile(CommonUtils.getRealFilePath(this, data.getData()));
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
} 最后把两个加载bitmap的方法贴出来:
/**
* 由string类型的路径加载图片
*
* @param context
* @param path
* @return
*/
public static Bitmap getBitmap(Context context, String path) {
Uri uri = Uri.fromFile(new File(path));
InputStream in = null;
try {
in = context.getContentResolver().openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
if (o.outHeight > 1024 || o.outWidth > 1024) {
scale = (int) Math.pow(
2,
(int) Math.round(Math.log(1024 / (double) Math.max(
o.outHeight, o.outWidth)) / Math.log(0.5)));
}
if (scale < 4) {
scale = 4;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
in = context.getContentResolver().openInputStream(uri);
Bitmap b = BitmapFactory.decodeStream(in, null, o2);
in.close();
return b;
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
return null;
}
/**
* 将图片的uri转为绝对路径
*
* @param context
* @param uri
* @return
*/
public static String getRealFilePath(final Context context, final Uri uri) {
if (null == uri)
return null;
final String scheme = uri.getScheme();
String data = null;
if (scheme == null)
data = uri.getPath();
else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
data = 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) {
data = cursor.getString(index);
}
}
cursor.close();
}
}
return data;
} 也 希望对有需要的战友有帮助。。。。
相关文章推荐
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- Android开发 调用系统相机相册图片功能,解决小米手机拍照或者图片横竖相反问题,及小米手机相册图片路径问题
- android 调用系统相册选取照片或者打开相机拍照获取图片
- android 调用系统相册选取照片或者打开相机拍照获取图片
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- 解决Android调用系统相机拍照后相片无法在相册中显示问题
- Android开发,调用相册图片或者调用相机进行拍照获取图片
- Android 调用系统相机拍照并且显示在相册中,以及中间可能会遇到的一些问题的解决
- 完美调用系统相机拍照,获取图片进行压缩并本地保存
- android中调用系统功能 来显示本地相册图片 拍照 视频 音频功能
- 解决三星调用系统相机拍照显示图片旋转90度横着的问题
- Android 拍照以及一些常用的处理,例如将图片显示到相册(包含了安卓系统6.0以上调用相机的处理)
- 调用系统相机拍照、调用相册、调用系统录像并获取缩略图
- 调用系统相机拍照,从相册选取图片上传
- Anroid调用系统相机或相册获取图片
- 调用相机拍照并得到图片-------得到系统相册中一张图片
- Android拍照调用系统相册仿微信封装总结,治疗各种崩溃,图片横竖问题压缩等问题。-更新2016-12-21
- iOS图片处理(一)调用系统相机和相册获取图片,给相机添加自定义覆盖物
- iOS开发swift如何调用系统相册和相机获取图片设置用户头像