Android 如何将位图设置为从相机捕获的 main.xml 中的 ImageView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2928904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:56:20  来源:igfitidea点击:

how to set the bitmap to the ImageView in main.xml captured from the camera?

androidbitmapimageview

提问by Srikanth Naidu

I have a Imageview in main.xml, how to set the bitmap the to the imageView in main.xml i can assign bitmap to the local image view in the below code.

我在 main.xml 中有一个 Imageview,如何将位图设置为 main.xml 中的 imageView 我可以在下面的代码中将位图分配给本地图像视图。

//Activates the Camera
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 1);

//  get the bitmap data from the Camera
Bundle extras = data.getExtras();
Bitmap b = (Bitmap) extras.get("data");
int width = b.getWidth();
int height = b.getHeight();
ImageView img = new ImageView(this);
img.setImageBitmap(b);

//Saves the  image
MediaStore.Images.Media.insertImage(getContentResolver(), b, timestamp, timestamp);

// Set the View
setContentView(img);

回答by sgarman

I'm having a little trouble understanding how you structured your app but here are my suggestions:

我在理解您如何构建应用程序时遇到了一些麻烦,但我的建议如下:

Change your setContentView(img);to setContentView(R.id.main);

改变你setContentView(img);setContentView(R.id.main);

Then do:

然后做:

ImageView mImg;
mImg = (ImageView) findViewById(R.id.(your xml img id));
mImg.setImageBitmap(img);