3

in my application one photo button is there. when I click on that button application open in camera mode and after capturing photo it will return on the layout and show that capture photo on this layout.But when the application is in camera mode and that time when I press return button then application stop working and it show :

The application "AppName" (XXX) has stopped unexpectedly [Force Close]

on the LogCat it show following error :

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=0, data=null} to activity {com.XXX.android.XXX/com.XXX.android.XXX.FirstActivity}: java.lang.NullPointerException

But here I want my application return to the layout. In my application I write following code : In main.xml I create one ImageView, one EditView, one photo button and one save button :

<ImageView 
   android:id="@+id/test_image"
   android:src="@drawable/aas"
   android:layout_width="180dp"
   android:layout_height="150dp"
   android:layout_below="@id/edit2"
   android:layout_toRightOf="@id/main_view_id_text7"
   android:layout_alignParentRight="true"
   android:layout_marginTop="5dp"
   android:layout_marginLeft="2dp" />
<EditText
    android:id="@+id/edit2"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:layout_below="@id/edit1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="9dp"
    android:layout_marginRight="4dp"
    android:layout_marginLeft="2dp"
    android:layout_toRightOf="@id/button1"
    android:layout_alignParentRight="false"
    android:background="@color/light_yellow"
    android:textColor="@color/darkblue"
    android:inputType="text"
    android:hint="Photo Name" />
<Button 
    android:id="@+id/button1"
    android:layout_width="70dp"
    android:layout_height="36dp"
    android:layout_below="@id/main_view_id_text4"
    android:layout_marginTop="11dp"
    android:onClick="addPhoto"
    android:text="@string/photo"
     />
<Button
     android:id="@+id/button2"
     android:layout_width="150dp"
     android:layout_height="40dp"
     android:layout_below="@id/test_image"
     android:layout_alignParentLeft="true"
     android:layout_marginTop="2dp" 
     android:onClick="savePhoto"
     android:text="@string/save" />

Now in Activity Class code camera application :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    editTextPhoto = (EditText) findViewById(R.id.edit2);

    buttonPhoto = (Button) findViewById(R.id.button1);

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

    buttonPhoto.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            addPhoto();
        }
    });

protected void addPhoto() {

    Intent cameraIntent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_REQUEST);

    editTextPhoto.setText(new Date().toString() + ".jpg");
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);
    }
}
protected void savePhoto() {
// code to save captured photo on provided location
}

I also want to show Image name on EditText (or in TextView), the image name which is captured by the Camera. By this code captured photo is stored in Gallery/Photo folder of mobile phone. One more thing I want which is when again I click on photo button the previous captured photo will delete from that folder and the captured photo will save only when I click on Save button.

One more thing please also mention how return button will handle so I use this in other part of application. In my application I collect some data. Suppose client enter some data and in the middle he/she press return button then one Alert box will come "your current data will loss what do you want [Yes] [No]" after pressing on "No" the application will stop.

List of Problem which I am facing :

  1. Return button not work while camera mode is open.
  2. Please mention how return button will handle so I use this in other part of application
  3. Retrieve the name of the photo which is captured by the Camera.
  4. Save only that photo for which I click on save button and other captured photo will not save on any other place.

Similar Question which asked earlier in this website, what they do: They Capture the photo by this code, save photo in gallery and display on the layout by ImageView but they not display the name of photo on the layout.

This message appear on the LogCat when I press return or back button of the mobile phone while application is in Camera mode:

01-23 10:47:25.839: E/AndroidRuntime(11266): FATAL EXCEPTION: main
01-23 10:47:25.839: E/AndroidRuntime(11266): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=0, data=null} to activity {com.pixel.android.locationentry/com.pixel.android.locationentry.FirstActivity}: java.lang.NullPointerException
        01-23 10:47:25.839: E/AndroidRuntime(11266):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at android.os.Handler.dispatchMessage(Handler.java:99)
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at android.os.Looper.loop(Looper.java:130)
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at android.app.ActivityThread.main(ActivityThread.java:3687)
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at java.lang.reflect.Method.invokeNative(Native Method)
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at java.lang.reflect.Method.invoke(Method.java:507)
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at dalvik.system.NativeStart.main(Native Method)
    01-23 10:47:25.839: E/AndroidRuntime(11266): Caused by: java.lang.NullPointerException
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at com.pixel.android.locationentry.FirstActivity.onActivityResult(FirstActivity.java:420)
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
    01-23 10:47:25.839: E/AndroidRuntime(11266):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
01-23 10:47:25.839: E/AndroidRuntime(11266):    ... 11 more
Vinit ...
  • 1,409
  • 10
  • 37
  • 66

2 Answers2

1

change your onActivityResult implementation in such a way that it can handle null response too (i.e. if you dont take pic and simply return). for example:

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

        if(photo != null)
             imageView.setImageBitmap(photo);
    }
}
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • Thanx Waqas here you want to say that when i press return button then it not capture any photo and my code inserting photo in ImageView which is not available that's why it show error when I press return button. – Vinit ... Jan 23 '12 at 08:22
  • ok guys... i got the solution on google.. actually here I need to put try catch block. we need to write whole code of onActivityResult method in try block. – Vinit ... Jan 23 '12 at 09:07
  • 1
    yes, you may avoid this either by writing the code block in try/catch or conditional statement (in case you know what could be the output) – waqaslam Jan 23 '12 at 09:11
  • ok thanx waqas ....here i also want to know the name of the photo. I want to display name of the photo and the photo which is captured by the camera. so please mention the code to get the name of the photo. Here I know photo is saved in gallery/camera folder of mobile phone and the photo name is current date and time but my question is how i get this photo name by code when i return to the layout to show the captured photo and its name. – Vinit ... Jan 23 '12 at 09:20
  • refer to http://stackoverflow.com/questions/6819985/get-file-from-uri-after-image-capture and http://stackoverflow.com/questions/4184951/get-path-of-image-from-action-image-capture-intent Here you have to provide a file-name yourself and then on returning from camera activity, get the file and do whatever you want to do. For a good practice, save your application's captured images in a separate folder (perhaps with your app's name) so that you dont add unnecessary pics in user's camera-shot folder – waqaslam Jan 23 '12 at 09:33
0

This is your problem:

01-23 10:47:25.839: E/AndroidRuntime(11266):    at com.pixel.android.locationentry.FirstActivity.onActivityResult(FirstActivity.java:420)

Analyze why its null.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45