Imageview不显示捕获的图像

时间:2016-10-29 07:29:43

标签: java android

我正在尝试将图像捕获并显示在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文件中我已经添加了相机的权限,所以这不是问题。

2 个答案:

答案 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.
    }
}