0

I had a class that access the native camera to take picture. After the picture had been taken, it will be save in a folder and that picture will be display in a new activity.

The problem is, i try to the get the data i put into the intent after the picture had been taken but it always says that the intent is null.pointer.exception. Below is my class, anyone please help me.

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

 public class CameraTestActivity extends Activity {
Button start;
final int TAKE_PICTURE = 2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    start = (Button)findViewById(R.id.startButton);

    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            File file = new File(Environment.getExternalStorageDirectory().toString() + "/testImage/" + "toBeUpload.jpg");
            Uri imageUri = Uri.fromFile(file);


            Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            cameraIntent.putExtra("path", imageUri.toString());
            startActivityForResult(cameraIntent,TAKE_PICTURE);

        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    super.onActivityResult(requestCode, resultCode, intent);
    if(resultCode == RESULT_OK)
    {
        if(requestCode == TAKE_PICTURE)
        {
            /*ERRROR OCCUR HERE*/
            Bundle extras = intent.getExtras();
        }
    }
}
}
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
chenhou90
  • 23
  • 7
  • check logcat and they give details in which line are error and try to solve it........check intent is null?? – Samir Mangroliya Sep 14 '11 at 15:02
  • possible duplicate of [Android ACTION_IMAGE_CAPTURE Intent](http://stackoverflow.com/questions/1910608/android-action-image-capture-intent) – Lukas Knuth Sep 14 '11 at 15:04

4 Answers4

1

Rich and userSeven7s are right, but it's rather besides the point in your particular case: you don't actually need the Intent's return data for the photo chooser (exception: some buggy very old phones only write a thumbnail in the extras and don't actually write an image to the path you specified, but that's another story) -- just look for the image written to the URI you passed the intent originally.

Yoni Samlan
  • 37,905
  • 5
  • 60
  • 62
  • can elborate a little about your last sentences "just look for the image written to the URI you passed the intent originally." Sorry, my english is not that good. – chenhou90 Sep 14 '11 at 15:43
  • You wrote this line: `cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);` That's where you'll find the full-sized image. – Yoni Samlan Sep 14 '11 at 17:32
  • do you mean i directly access the Uri but not getting the Uri from the intent? That could work but i still don't get the reason why the intent would be null. – chenhou90 Sep 14 '11 at 23:48
  • Is the intent null, or the extra data null? If it's just the extra data, see http://stackoverflow.com/questions/1910608/android-action-image-capture-intent (which Lukas marked as a duplicate). – Yoni Samlan Sep 15 '11 at 15:02
  • the intent is null not the extra data. – chenhou90 Sep 19 '11 at 14:50
0

I wonder if the call to super.onActivityResult is consuming the Intent instance and setting it to null when it's through with it. You don't need that call to the super class since you're implementing it yourself, so give that a try.

Rich
  • 36,270
  • 31
  • 115
  • 154
0

Call the super.onActivityResult(requestCode, resultCode, intent); after you process the event.

Ron
  • 24,175
  • 8
  • 56
  • 97
0

Try:

startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

Referencing: http://achorniy.wordpress.com/2010/04/26/howto-launch-android-camera-using-intents/

JoeyG
  • 611
  • 6
  • 14
  • The request code you use is purely for internal reference (so you can disambiguate exactly what activity you started is returning a result). Using a different value won't change the actual returned intent, only the request code it comes back with. – Yoni Samlan Sep 15 '11 at 14:56