1

I have problems when i use some code from Capture Image from Camera and Display in Activity and https://developer.android.com/training/camera/photobasics.html

First, this is in MainActivity.java. I create onClickevent when user press button "snap"

 Button snap = (Button) findViewById(R.id.button3);
    snap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent start_cam = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File path = null;
            try {
                path = createImageFile();
            }
            catch(Exception e)
            {
                Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
            }
            if(path!=null) {
                start_cam.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(path));
                startActivityForResult(start_cam, 1);
            }

        }

    });

Next, i add createImageFile() method

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 = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  // prefix
            ".jpg",         // suffix
            storageDir      // directory
    );

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

Last part, I add onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {
        try {
            m = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(root_file));
            try {
                view.setImageBitmap(m);
            }
            catch(Exception e)
            {
                Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I also add heading in my AndroidManifest.xml

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

After, i use this feature in android emulator API19 Nexus_one API19 (take a photo). The dialog appears and said "java.lang.nullPointerException".

I don't know why this problem appears. I think it may be my emulator or code.

Community
  • 1
  • 1

0 Answers0