简体   繁体   English

android studio 无法在图像视图中显示图像

[英]android studio cannot show the image in the image view

//global variable //全局变量

static final int CAMERA_REQUEST = 1;
ImageView imageview;
Bitmap bitmap;

//on create: //在创建时:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

//take reference //参考

imageview = findViewById(R.id.image_view);

//the onclick listener and the intent: //点击监听器和意图:

Button take_photo_btn = findViewById(R.id.photo_btn);
take_photo_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_REQUEST);
    }
});

//this is the activity for result: //这是结果的活动:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_REQUEST && requestCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        bitmap = (Bitmap) extras.get("image");
        imageview.setImageBitmap(bitmap);
    }
}

after i take a photo i didnt see it in the image view whats is the problem?拍照后,我没有在图像视图中看到它,这是什么问题?

If you are using Marshmallow then you have to implement Runtime Permissions for Camera using in Android.如果您使用的是 Marshmallow,那么您必须在 Android 中实现相机的运行时权限。

More details go this link https://developer.android.com/training/permissions/requesting更多详情请访问此链接https://developer.android.com/training/permissions/requesting

Assuming that you have included the camera permission in your Manifest:假设您在 Manifest 中包含了相机权限:

<uses-permission android:name="android.permission.CAMERA" />

Try to check if the user has already granted permission to use the camera:尝试检查用户是否已经授予使用相机的权限:

if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED)

Then this may solve your problem in Marshmallow:那么这可能会解决您在棉花糖中的问题:

requestPermissions(activity, new String[] {Manifest.permission.CAMERA}, requestCode);

Ok i found the problem here:好的,我在这里发现了问题:

if (requestCode == CAMERA_REQUEST && requestCode == RESULT_OK)

I need to do this:我需要这样做:

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM