我正在尝试将图像捕获并显示在Android Studio上的ImageView
中,但它只是捕获并且不显示图像。
CODE:
public class MainActivity extends AppCompatActivity {
Button click;
private static final int camRequest = 1;
ImageView imageView;
Bitmap bit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = (Button)findViewById(R.id.button);
click.setOnClickListener(delListener);
imageView = (ImageView)findViewById(R.id.imageView);
}
public View.OnClickListener delListener = new View.OnClickListener() {
public void onClick (View view){
try {
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, camRequest);
}
catch(ActivityNotFoundException e){
Log.e("", "errpr");
}
}};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult( requestCode, resultCode, data);
if (resultCode == camRequest) {
Bitmap bit2 = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bit2);
}
}
我想我一直都做得很好,在Manifest
文件中我已经添加了相机的权限,所以这不是问题。
答案 0 :(得分:1)
错误在:
if (resultCode == camRequest)
resultCode
使用 Activity
来显示结果是OK
还是ERROR
。
将其替换为:
if (requestCode == camRequest && resultCode == Activity.RESULT_OK)
答案 1 :(得分:1)
结果代码不应该在那里进行比较,正确的方法:
...
// check if request code is camRequest
if(requestCode == camRequest){
if(resultCode == Activity.RESULT_OK){
// If result is okay...do something
} else {
// there was a failure.
}
}