Error with image view when displaying an image by camera capturing

811 views Asked by At

i am new to android and i am trying to make my phone cam take a picture and display it in an image view. When the image is captured, it is saved correctly but after that the app stops working when the image should be displayed in an image view. Any help is appreciated. I searched some topics but still nothing works. Here is the code:

package myfirstapp.myapps.me.camera;

import ...

public class MainActivity extends ActionBarActivity {
    ImageButton camBtn;
    ImageView imageView;
    ScrollView scrollView;

    private File imageFile;

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

    public void OpenCam(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        imageFile=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"image.jpg");
        Uri tempURI=Uri.fromFile(imageFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0)//==0 the same where startActivityForResult(intent, 0) so we are in the same process
        {


            switch (resultCode){
                case Activity.RESULT_OK:
                    if(imageFile.exists())
                    {
                        Toast.makeText(MainActivity.this, "Image was saved at "+imageFile.getAbsolutePath(), Toast.LENGTH_SHORT)
                                .show();
                        Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

                        ImageView myImage = (ImageView) findViewById(R.id.imageView);

                        myImage.setImageBitmap(myBitmap);
                    }
                    else
                    {
                        Toast.makeText(MainActivity.this, "Image wasn't saved", Toast.LENGTH_SHORT)
                                .show();
                    }
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(MainActivity.this, "Image capture was cancelled", Toast.LENGTH_SHORT)
                            .show();
                    break;
            }
        }
    }
}

Stacktrace

Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {myfirstapp.myapps.me.camera/myfirstapp.myapps.me.camera.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.deliverResults(ActivityThread.java:3410) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2859)

3

There are 3 answers

3
Shoeb Siddique On BEST ANSWER

Please go through this Code:

    public static final int REQUEST_IMAGE_CAPTURE = 1;
    public static final int RESULT_LOAD_IMAGE = 10;
     private Bitmap myBitmap;
     ImageView myImage ;
        public void OpenCam(View view) {

              Intent takePictureIntent = new             Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if     (takePictureIntent.resolveActivity(getPackageManager()) != null) 
                    {

                        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                    }
    }

And also,

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


        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) 
        {
            Bundle extras = data.getExtras();
            myBitmap= (Bitmap) extras.get("data");

             myImage = (ImageView) findViewById(R.id.imageView);

                    myImage.setImageBitmap(myBitmap);
        }

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            myBitmap= BitmapFactory.decodeFile(picturePath);

            //          photoUri = picturePath;



        myImage.setImageBitmap(myBitmap);

        }

    }
2
Jibran Khan On

Why are you first saving and trying to show the saved image file. Use the returned data from onActivityResult. Before file is actually created in the memory, broadcast is sent so it can refresh the directory and show the new file. But better is to use what camera intent is returning as data.

Replace your this code

Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

                        ImageView myImage = (ImageView) findViewById(R.id.imageView);

                        myImage.setImageBitmap(myBitmap);

with this

ImageView myImage = (ImageView) findViewById(R.id.imageView);
Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                myImage.setImageBitmap(photo);
3
anu On

first : comment the line intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // no need to write for capture camera

second : use this line

imageFile=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/abc); // abc folder name

instead of

imageFile=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"image.jpg");

Note : your code work upto kitkat but lollipop it will crash because in lollipop imageFile return null so Uri failed to convert in file . Hope your problem will resolve . and one more thing before open camera u should check that sdcard permission .