在做项目是于到的问题,现在有时间了总结一下:
在做拍照时不同机型拍照,开始是总是不能用于测试的机子,最后通过查资料,实践总结的方法。该方法适用于 小米,三星,华为,联想,小辣椒
点击拍照或本地获取图片
case R.id.btn_take_photo:
File file = new File(Environment.getExternalStorageDirectory(),"textphoto.jpg");
outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CONSULT_DOC_CAMERA);
break;
case R.id.btn_pick_photo:
Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
intent1.addCategory(Intent.CATEGORY_OPENABLE);
intent1.setType("image/*");
startActivityForResult(intent1, CONSULT_DOC_PICTURE);
break;
获取结果
/**
* 接受数据
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CONSULT_DOC_PICTURE) {
if (data == null) {
return;
}
Uri uri = data.getData();
if (uri != null) {
try {
Log.i("url-------------", uri.toString());
ContentResolver resolver = getContentResolver();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.RGB_565;
options.inJustDecodeBounds = true;
bmp = BitmapFactory.decodeStream(resolver.openInputStream(uri),null,options);
// bmp = MediaStore.Images.Media.getBitmap(resolver, uri);//显得到bitmap图片
int mWidth = options.outWidth;
int mHeight = options.outHeight;
if (bmp!=null&&!bmp.isRecycled()) {
bmp.isRecycled();
}
int sWidth = ImageHelper.getScreenMetrics(ShuoShuoActivity.this).x;
int sHeight = ImageHelper.getScreenMetrics(ShuoShuoActivity.this).y;
/*
* 判断图片的大小,大图片铺满屏幕,小图片自适应
*/
if( mWidth > sWidth || mHeight > sHeight ) {
int s = 1;
while ((mWidth / s > sWidth * 2) || (mHeight / s > sHeight * 2))
{
s *= 3;
}
options = new BitmapFactory.Options();
options.inSampleSize = s;
zoomBitmap = BitmapFactory.decodeStream(resolver.openInputStream(uri), null, options);
} else {
zoomBitmap = BitmapFactory.decodeStream(resolver.openInputStream(uri));
}
Matrix m = new Matrix();
String[] proj = {MediaStore.Images.Media.DATA};
String path ="";
Cursor cursor = resolver.query(uri, proj, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//最后根据索引值获取图片路径
path = cursor.getString(column_index);
cursor.close();
m.postRotate(getExifOrientation(path));
Log.i("shuoshuoImagePic---", ""+uri.toString());
zoomBitmap = Bitmap.createBitmap(zoomBitmap, 0, 0, zoomBitmap.getWidth(), zoomBitmap.getHeight(), m, false);
}else{
// path = uri.getEncodedPath();
Log.i("imagepath", path);
zoomBitmap = MediaStore.Images.Media.getBitmap(resolver, uri);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
talkImage.setImageBitmap(zoomBitmap);
talkImage.setBackgroundResource(0);
}else if (requestCode == CONSULT_DOC_CAMERA) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.RGB_565;
options.inJustDecodeBounds = true;
bmp = BitmapFactory.decodeFile(outputFileUri.getPath(),options);
int mWidth = options.outWidth;
int mHeight = options.outHeight;
if (bmp!=null&&!bmp.isRecycled()) {
bmp.isRecycled();
}
int sWidth = ImageHelper.getScreenMetrics(ShuoShuoActivity.this).x;
int sHeight = ImageHelper.getScreenMetrics(ShuoShuoActivity.this).y;
/*
* 判断图片的大小,大图片铺满屏幕,小图片自适应
*/
if( mWidth>sWidth || mHeight>sHeight ) {
int s = 1;
while ((mWidth / s > sWidth * 2) || (mHeight / s > sHeight * 2))
{
s *= 3;
}
options = new BitmapFactory.Options();
options.inSampleSize = s;
options.outWidth = sWidth;
options.outHeight = sHeight;
zoomBitmap = BitmapFactory.decodeFile(outputFileUri.getPath(),options);
if (zoomBitmap == null) {
return;
}else {
Matrix m = new Matrix();
m.postRotate(getExifOrientation(outputFileUri.getPath()));
zoomBitmap = Bitmap.createBitmap(zoomBitmap, 0, 0, zoomBitmap.getWidth(), zoomBitmap.getHeight(), m, false);
talkImage.setImageBitmap(zoomBitmap);
talkImage.setBackgroundResource(0);
}
}
outputFileUri=null;
}
else {
Toast.makeText(ShuoShuoActivity.this, "请重新选择图片", Toast.LENGTH_SHORT)
.show();
}
}