-1

I made an app where you use the default camera to take a photo then displays it on the image view.Problem is, image does not show in the image view. Tried many ways but no solution.

mainactivity.java:

public class MainActivity extends Activity {

    private static final int ACTIVITY_START_CAMERA_APP = 0;
    static final int REQUEST_IMAGE_CAPTURE = 1;
    private ImageView mPhotoCapturedImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPhotoCapturedImageView = (ImageView) findViewById(R.id.capturePhotoImageView);
    }

    public void takePhoto(View view){
        Intent callCameraApplicationIntent = new Intent();
        callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast.makeText(this, "Picture taken sucessfully!", Toast.LENGTH_SHORT).show();
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            mPhotoCapturedImageView.setImageBitmap(imageBitmap);
        }
    }
}

main_activity.xml:

<?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.taufiq.ocrdemo.MainActivity">

    <ImageView
        android:id="@+id/capturePhotoImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/photoButton"
        android:layout_marginBottom="19dp"
        android:layout_marginEnd="43dp"
        android:layout_marginLeft="43dp"
        android:layout_marginRight="43dp"
        android:layout_marginStart="43dp"
        android:layout_marginTop="16dp"
        android:contentDescription="@string/preview"
        android:minHeight="300dp"
        app:layout_constraintBottom_toTopOf="@+id/photoButton"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1"
        tools:minHeight="300dp" />

    <Button
        android:id="@+id/photoButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="takePhoto"
        android:text="@string/capture_photo"
        android:layout_weight="1"
        tools:ignore="MissingConstraints"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="27dp"
        app:layout_constraintLeft_toLeftOf="parent" />

</android.support.constraint.ConstraintLayout>
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44

3 Answers3

1

Problem is here :

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

you are starting the Activity with requestCode ACTIVITY_START_CAMERA_APP

startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP);

so you need to modify the if condition like:

 if (requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) {
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
1

Well it wont you know because the id you passed for capture and activity result is different, startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP);from requestCode == REQUEST_IMAGE_CAPTURE they ofto match.

Remario
  • 3,813
  • 2
  • 18
  • 25
1

debug inside this: maybe it isnt insert this if :

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

by the way some device not give image inside data. So you have to give path before image take. Here is sample :

firstly add this inside class :

private Uri uriFilePath;

then add this somewhere inside your class:

you have to store this because camera is the new intent

@Override
    protected void onSaveInstanceState(Bundle outState) {

        if (uriFilePath != null) outState.putString("uri_file_path", uriFilePath.toString());
        super.onSaveInstanceState(outState);
    }

add this onCreate :

 if (savedInstanceState != null) {
            if (uriFilePath == null && savedInstanceState.getString("uri_file_path") != null) {
                uriFilePath = Uri.parse(savedInstanceState.getString("uri_file_path"));
            }


        }

here is capture function :

private void captureImage() {
    PackageManager packageManager = this.getPackageManager();
    if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        File mainDirectory = new File(Environment.getExternalStorageDirectory(), "Tofaş");//tmp
        if (!mainDirectory.exists())
            mainDirectory.mkdirs();

        Calendar calendar = Calendar.getInstance();
        cameraFileName = "IMG_" + calendar.getTimeInMillis()+".jpg";
        uriFilePath = Uri.fromFile(new File(mainDirectory, cameraFileName));
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uriFilePath);
        startActivityForResult(intent, RC_CAMERA);

    }

}

then use this for get image inside onActivityResult :

   if (requestCode == RC_CAMERA && resultCode == RESULT_OK) {


      uriFilePath.getPath()// is the path of your image


            return;
        }
Canberk Çakmak
  • 207
  • 1
  • 4
  • 16