Skip to content Skip to sidebar Skip to footer

Can't Get Camera Code From Android Docs To Work

I am using code from http://developer.android.com/training/camera/photobasics.html Code: private void dispatchTakePictureIntent() { Intent takePictureIntent = new Inten

Solution 1:

That's one of notorious Android development experience.

Android Intent does not guarantee to give captured image in data.getExtras(), especially user utilize 3rd party camera/imaging app. You can find many trials and suggestions in here and anywhere googled with "android camera intent null".

Some common of them are as below.

  1. data.getExtras().get("data");
  2. data.getExtras() with different key (i.e "photo")
  3. data.getData()
  4. Uri.fromFile(f) for EXTRA_OUTPUT predefined path.
  5. Uri.fromFile(f) with some random filename (datetime format or IMG-xxx) without maintaining EXTRA_OUTPUT definition.

I recommend you to find it using breakpoint which route of the variable that the intent given. It would be good to check all of them in if-else if-else approach.

In addition, check out crash report carefully after releasing the app. You may get the error out of the above trials.


Solution 2:

To get the ThumbNail, you don't need to create a file etc. please try this code below.

   Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // Ensure that there's a camera activity to handle the intent
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                }

and to get the results.

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                imgView.setImageBitmap(imageBitmap);
            }

    }

imgView is the ImageView you want to set the ThumbNail to.

In case if you want to create a file and then try this, [ which is not needed for a Thumbnail], you may want to try adding the following permission to manifest as you are trying to read and write to storage.

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

All the best.


Post a Comment for "Can't Get Camera Code From Android Docs To Work"