Android Camera : data intent returns null

前端 未结 11 1735
不知归路
不知归路 2020-11-22 16:20

I have an android application which contains multiple activities.

In one of them I\'m using a button which will call the device camera :

public void         


        
11条回答
  •  生来不讨喜
    2020-11-22 16:54

    I´ve had experienced this problem, the intent is not null but the information sent via this intent is not received in onActionActivit()

    This is a better solution using getContentResolver() :

        private Uri imageUri;
        private ImageView myImageView;
        private Bitmap thumbnail;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
          ...
          ...    
          ...
          myImageview = (ImageView) findViewById(R.id.pic); 
    
          values = new ContentValues();
          values.put(MediaStore.Images.Media.TITLE, "MyPicture");
          values.put(MediaStore.Images.Media.DESCRIPTION, "Photo taken on " + System.currentTimeMillis());
          imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
          Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
          startActivityForResult(intent, PICTURE_RESULT);
    
      }
    

    the onActivityResult() get a bitmap stored by getContentResolver() :

     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if (requestCode == REQUEST_CODE_TAKE_PHOTO && resultCode == RESULT_OK) {
    
                Bitmap bitmap;
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
                    myImageView.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
        }
    

    Check my example in github:

    https://github.com/Jorgesys/TakePicture

提交回复
热议问题