-3
package com.example.android.health3;

import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    ImageView result;
    static final int REQUEST_IMAGE_CAPTURE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        result = (ImageView) findViewById(R.id.imageView);

    }
    public void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    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");
            result.setImageBitmap(imageBitmap);
        }
    }

}

It is supposed to be a very simple concept. I did exactly what it says on the documentation from android developer, but this code just doesn't run... The app stops when i press the button to take a picture.

Please let me know if you know why. :(

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.health3.MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="dispatchTakePictureIntent"
        android:text="Button"
      />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="208dp"
        android:layout_height="231dp"
     />

</android.support.constraint.ConstraintLayout>

Documentation: https://developer.android.com/training/camera/photobasics.html

Justin Chan
  • 49
  • 1
  • 6

3 Answers3

1

use this

public void dispatchTakePictureIntent(View v) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
}

insted of this

public void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

don't forgot add permission in manifest file

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

NOTE :

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app

for more information read from docs

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Hi thank you so much. It works now. But may I know why you have to put view as an argument there? cuz in the documentation, it doesn't take an argument. – Justin Chan Sep 16 '17 at 09:15
  • @JustinChan check this https://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene and this also https://stackoverflow.com/a/21326604/7666442 – AskNilesh Sep 16 '17 at 09:24
  • Hi. @NileshRathod how do i use the `View` parameter within `dispatchTakePictureIntent` and which view should should I use (ImageView)? – Kellin Strook Jul 14 '20 at 13:52
1

Try this one

Call this function takePicture on Button Onclick

 private void takePicture() {


    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");

    String dateCreated = sdf.format(new Date(System.currentTimeMillis()));

    f = new File(getExternalCacheDir(), "IMGPLk-" + dateCreated.trim() + ".jpg");

    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

    //intent.putExtra("android.intent.extras.CAMERA_FACING", 1);

    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);

    //finish();
}
Harshal Deshmukh
  • 1,787
  • 3
  • 14
  • 25
0

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST);

NipunPerfect
  • 125
  • 1
  • 7