Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Taking a picture using camera intent and saving it to a java.io.File
    primarykey
    data
    text
    <p>I'm trying to get a picture from user and then send it to our server. I provide two options: choosing from gallery or taking a new picture using default camera in their phones.</p> <p>Gallery thing works correctly, I can get the photo they selected and send it without any problem. But in camera option, camera opens, takes a picture, saves it on SD card with the location I specified, no problem, but when it returns back to my application, I always get a NullPointerException and my application is forced to close. Here is what I do:</p> <pre><code>private String tempFilePath = ""; ... public void addPhotoUsingCamera() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); tempFilePath = getOutputMediaFilePath(MEDIA_TYPE_IMAGE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(tempFilePath))); startActivityForResult(intent, CAMERA_ACTIVITY_CODE); } </code></pre> <p>And then I use following to get the photo taken:</p> <pre><code>@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { ... case CAMERA_ACTIVITY_CODE: if (resultCode == Activity.RESULT_OK) { picture = new File(tempFilePath); } break; ... } } </code></pre> <p>I also tried</p> <pre><code>Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); ImageView image = (ImageView) findViewById(R.id.photoTaken); image.setImageBitmap(thumbnail); </code></pre> <p>inside <code>case CAMERA_ACTIVITY_CODE:</code> but no matter what I try I always get a NullPointerException in this part of code.</p> <p>There is the logcat info where line 202 corresponds to <code>case CAMERA_ACTIVITY_CODE:</code> part.</p> <pre><code>01-31 23:40:13.154: ERROR/AndroidRuntime(8009): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=101, result=-1, data=null} to activity {com.ollaa.android/com.ollaa.android.share.ShareFinalScreen}: java.lang.NullPointerException 01-31 23:40:13.154: ERROR/AndroidRuntime(8009): Caused by: java.lang.NullPointerException 01-31 23:40:13.154: ERROR/AndroidRuntime(8009): at com.ollaa.android.share.ShareFinalScreen.onActivityResult(ShareFinalScreen.java:202) </code></pre> <p>Any help is greatly appreciated.</p> <p><strong>Note:</strong> I have declared all necessary permissions in AndroidManifest related to camera and SD card.</p> <p><strong>EDIT:</strong> I realized that tempFilePath does not last until camera activity returns to my app. I put the initialization for tempFilePath in my <code>onCreate</code> method but this time picture is not set when I try to reach it from an outside function other than <code>onActivityResult</code>. I feel like I do not know Java at all, when we change a class variable within a function, the changed value should be visible from all functions of that class right??</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload