0

I'm new to Android and am trying to use the camera and show a full-size image rather than bitmap. I've been following the [Android tutorials for this] but the app keeps crashing when I click the button. (https://developer.android.com/training/camera/photobasics.html#TaskCaptureIntent)

Here's my MainActivity

public class MainActivity extends AppCompatActivity {

ImageView imageView;
static final int REQUEST_TAKE_PHOTO = 1;
String mCurrentPhotoPath;

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

    Button btnCamera = (Button)findViewById(R.id.btnCamera);
    imageView = (ImageView)findViewById(R.id.imageView);

    btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // Ensure that there's a camera activity to handle the intent
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                // Create the File where the photo should go
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    // Error occurred while creating the File
                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), "com.example.bryanjordan.camera_round_three.fileprovider", photoFile);
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                    startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                }
            }
            startActivityForResult(takePictureIntent,0);
        }
    });
}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}}

And for MainActivity layout:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/btnCamera"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Any help would be greatly appreciated!

Hews
  • 569
  • 6
  • 19
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Sep 01 '17 at 15:01
  • What crash do you get? It could be your FileProvider, or onActivityResult(). On some devices, the camera ignores the EXTRA_OUTPUT. – Alex Cohn Sep 01 '17 at 16:20
  • It's java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference for this line Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), "com.example.bryanjordan.camera_round_three.fileprovider", photoFile); – Hews Sep 01 '17 at 22:31
  • Had the incorrect argument for authorities under Manifest.xml and got everything working in the end. How do I skip showing a bitmap version of the photo once it's been taken and instead show the full-size image? Cheers – Hews Sep 02 '17 at 04:50

0 Answers0