使用Intent单击图像并启动Android Camera时出现问题。我通过意图数据获得的图像包含调整大小的位图图像的信息。也许我有错误的理解,但请建议我该怎么做才能纠正它。 ImageView显示我点击的相同图像,但显示的图像非常模糊
以下是基础代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
InputStream stream= null;
super.onActivityResult(requestCode, resultCode, data);
if(this.requestCode == requestCode && resultCode == RESULT_OK){
try{
//stream= getContentResolver().openInputStream(data.getData());
Bundle extras= data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
imageHolder.setImageBitmap(bitmap);
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
答案 0 :(得分:0)
try{
//stream= getContentResolver().openInputStream(data.getData());
Bundle extras= data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
//set value Width=200, Height=200
Bitmap resizedimg = Bitmap.createScaledBitmap(bitmap, Width, Height, true);
imageHolder.setImageBitmap(resizedimg);
}
catch (Exception ex){
ex.printStackTrace();
}
答案 1 :(得分:0)
也许你应该将uri传递给相机,并在ActivityResult而不是位图时从中获取图像
答案 2 :(得分:0)
试试这个:
public class MainActivity extends Activity {
static int CAMERA_TAKE_PIC =1;
Uri outPutfileUri;
ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.image);
}
public void CameraClick(View v) {
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"Photo.jpg");
outPutfileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(intent, CAMERA_TAKE_PIC);
}
Bitmap bitmap = null;
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data)
{
if (requestCode == CAMERA_TAKE_PIC && resultCode==RESULT_OK) {
String uri = outPutfileUri.toString();
Log.e("uri-:", uri);
Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show();
//Bitmap myBitmap = BitmapFactory.decodeFile(uri);
// mImageView.setImageURI(Uri.parse(uri)); OR drawable make image strechable so try bleow also
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), outPutfileUri);
Drawable d = new BitmapDrawable(getResources(), bitmap);
mImageView.setImageDrawable(d);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我认为图像模糊,因为意图数据仅包含图像的thumbnail
。您可以将图像保存在内部/外部存储上,然后获取它。
答案 3 :(得分:0)
图像可能模糊,因为意图数据仅包含图像的缩略图。您必须将图像保存在内部/外部存储器上,然后才能获取它。此外,您的问题应该是如何从相机获取全尺寸图像。 完成此文档https://developer.android.com/training/camera/photobasics.html。
这是应该如何做的
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
output=new File(dir, "CameraContentDemo.jpeg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
参考可以在此链接中找到。