0

i am implementing a MMS application for that i am using device camera.After taken photo i am getting bellow exception after press save in image display screen.

06-06 17:27:13.640: E/AndroidRuntime(21896): FATAL EXCEPTION: main
06-06 17:27:13.640: E/AndroidRuntime(21896): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=500, result=-1, data=null} to activity {com.example.nirbhaya/com.example.nirbhaya.TakePic}: java.lang.NullPointerException
06-06 17:27:13.640: E/AndroidRuntime(21896):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
06-06 17:27:13.640: E/AndroidRuntime(21896):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
06-06 17:27:13.640: E/AndroidRuntime(21896):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
06-06 17:27:13.640: E/AndroidRuntime(21896):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
06-06 17:27:13.640: E/AndroidRuntime(21896):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 17:27:13.640: E/AndroidRuntime(21896):    at android.os.Looper.loop(Looper.java:130)
06-06 17:27:13.640: E/AndroidRuntime(21896):    at android.app.ActivityThread.main(ActivityThread.java:3687)
06-06 17:27:13.640: E/AndroidRuntime(21896):    at java.lang.reflect.Method.invokeNative(Native Method)
06-06 17:27:13.640: E/AndroidRuntime(21896):    at java.lang.reflect.Method.invoke(Method.java:507)
06-06 17:27:13.640: E/AndroidRuntime(21896):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-06 17:27:13.640: E/AndroidRuntime(21896):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-06 17:27:13.640: E/AndroidRuntime(21896):    at dalvik.system.NativeStart.main(Native Method)
06-06 17:27:13.640: E/AndroidRuntime(21896): Caused by: java.lang.NullPointerException
06-06 17:27:13.640: E/AndroidRuntime(21896):    at com.example.nirbhaya.TakePic.onActivityResult(TakePic.java:73)
06-06 17:27:13.640: E/AndroidRuntime(21896):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
06-06 17:27:13.640: E/AndroidRuntime(21896):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
06-06 17:27:13.640: E/AndroidRuntime(21896):    ... 11 more

and code ::

package com.example.nirbhaya;

public class TakePic extends Activity {
    private static final int CAMERA_REQUEST = 500;
    private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.takepic);

    this.imageView = (ImageView) findViewById(R.id.imgLoad);
    Button bt = (Button) findViewById(R.id.btCam);
    Button picok = (Button) findViewById(R.id.bPicOk);

    bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; 
            File newdir = new File(dir); 
            newdir.mkdirs();
         int count = 0;
            // here,counter will be incremented each time,and the picture taken by camera will be stored as 1.jpg,2.jpg and likewise.
            count++;
            String file = dir+count+".jpg";
            String photo = file;
            File newfile = new File(file);
            try {
                newfile.createNewFile();
            } catch (IOException e) {}       

            Uri outputFileUri = Uri.fromFile(newfile);

            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

            startActivityForResult(intent, CAMERA_REQUEST);

        }
    });
 }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);
    }
}
}

please help me to solve. Thanks

Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69
KCRaju
  • 544
  • 3
  • 7
  • 21

2 Answers2

0

You are getting wrong because you are doing it wrong way.

If you pass the extra parameter MediaStore.EXTRA_OUTPUT with the camera intent then camera activity will write the captured image to that path and it will not return the bitmap in the onActivityResult method.

If you will check the path which you are passing then you will know that actually camera had write the captured file in that path.

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
0

I was facing same problem but fixed using following code ,

 Write folllowing code before `onCreate()`

 File output;

Write Following code inside onClickListner

 Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
 output = new File(dir, "CameraContentDemo.jpeg");
 i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
 startActivityForResult(i, 1);

Inside onActivityResult

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

    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
           imgUpload.setImageURI(Uri.fromFile(output));
         }
}

Reference Links:

Answer given by CommonsWare

Sample project provided by CommonsWare

Community
  • 1
  • 1
Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69
  • i need last image uri and need to set that image in imageview – KCRaju Jun 06 '13 at 15:53
  • last image ? can u explain this ? – Ronak Mehta Jun 07 '13 at 05:06
  • I am implementing one small MMS application.For that i am using device camera.When ever i launch the application it show a screen with 2 Buttons and one ImageView.First button for open camera and second button for send MMS.After press Button1 Camera is opening and saving picture in memory.But i need that "photo id" to send SMS. – KCRaju Jun 07 '13 at 08:00
  • for this you should ask new question cause this is totally different – Ronak Mehta Jun 07 '13 at 08:02
  • While doing the above process i am getting that exception :( – KCRaju Jun 07 '13 at 10:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31394/discussion-between-kcraju-and-rstar) – KCRaju Jun 07 '13 at 11:49