10

I have two options "select photo" and "take photo" - I have my select photo functionality fully working, but having problems with taking a photo. Mainly having the saved image display in my image view, after its been saved.

Defined my photo location:

public class photoActivity extends Activity {   
      private String photoPath;
      private ImageView imgView;
{...} 

My camera listener:

bPicFromCam.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String state = Environment.getExternalStorageState();

            if (Environment.MEDIA_MOUNTED.equals(state)) {

                long captureTime = System.currentTimeMillis();

                photoPath = Environment.getExternalStorageDirectory() + "/MYAPP" + captureTime + ".jpg";

                getPicFromCam(v);
            }
            else{

                Toast.makeText(getApplicationContext(),
                    "Sorry there is a problem accessing your SDCard, " +
                    "please select a picture from your gallery instead.", Toast.LENGTH_LONG).show();
            }

        }
    });

Then my code to start the camera intent (note that the photoPath is correct):

    public void getPicFromCam(View view){

    System.out.println("photoPath: " + photoPath);
    //Outputs the CORRECT location!

    try{

        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File photo = new File(photoPath);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(photo));
        imageUri = Uri.fromFile(photo);
        startActivityForResult(Intent.createChooser(intent, "Capture Image"), CAPTURE_IMAGE);

    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.exception_message),
                Toast.LENGTH_LONG).show();
        Log.e(e.getClass().getName(), e.getMessage(), e);
    }
}

OK everything seems fine up until this point - the picture is taken and the image gets saved to the specified location.

Now I am trying to display the image in my Image view (then it can be uploaded after the user confirms its good).

My onActivityResult:

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

    switch (requestCode) {
    case PICK_IMAGE:
        if (resultCode == Activity.RESULT_OK) {
            //THIS WORKS
        }
        break;

    case CAPTURE_IMAGE:         

        if (resultCode == RESULT_CANCELED) {
          Toast toast = Toast.makeText(this,"Canceled, no photo selected.", 1000);
          toast.show();
          return;
         }

        if (requestCode == CAPTURE_IMAGE && resultCode == RESULT_OK)
         {

            try {

                System.out.println("photoPath " + photoPath);
                //This is NULL!!!  And my problem, halp!

                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();  
                bitmapOptions.inSampleSize = 6;  
                bitmap = BitmapFactory.decodeFile(photoPath, bitmapOptions);

                imgView.setImageBitmap(bitmap);


            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Internal error",
                        Toast.LENGTH_LONG).show();
                Log.e(e.getClass().getName(), e.getMessage(), e);

            }

         }

        break;
    default:
    }
}

Thats the way I am currently trying it, but I have also tried using the following in my onActivityResult:

bitmap = (Bitmap) data.getExtras().get("data");

But every time I try and variation of the above method I get a NPE.

EDIT:

    03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): null
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): java.lang.NullPointerException
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at com.myco.photoapp.SelectPhoto.onActivityResult(SelectPhoto.java:277)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.Activity.dispatchActivityResult(Activity.java:3890)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3115)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.os.Looper.loop(Looper.java:123)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at java.lang.reflect.Method.invokeNative(Native Method)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at java.lang.reflect.Method.invoke(Method.java:521)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at dalvik.system.NativeStart.main(Native Method)

Above NPE comes when:

//below is line 277
String result = data.toURI();

                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();  

                bitmapOptions.inSampleSize = 6;  
                bitmap = BitmapFactory.decodeFile(result, bitmapOptions);
bMon
  • 962
  • 4
  • 15
  • 33
  • 1
    Oh god, 9PM is rapidly approaching... – bMon Mar 12 '11 at 00:46
  • Hi bMon.. I am facing problem exactly like you.. Could you please update your answer.. I am really weird.. If you could please see my [post](http://stackoverflow.com/questions/11099886/failure-delivering-result-resultinfowho-null-request-77-result-1-data-null).. Ur answer will really help me..+1 for you question – vinothp Jun 19 '12 at 11:48

4 Answers4

23

Your Activity is probably being destroyed and re-created when the camera activity is going off. Maybe try saving photoPath into the Bundle in onSaveInstanceState and then fish it back out in onCreate (make sure to check for nulls in onCreate when you do so)?

jjb
  • 3,560
  • 1
  • 21
  • 30
  • Hi jjb.. I am facing the same problem.. could you please update your answer how to do that.. +1 for answer – vinothp Jun 19 '12 at 11:52
  • The problem is that the Activity can be destroyed when another activity (the camera one, in this case) starts. You can save off information in onSaveInstanceState to recover what it was the user was doing when you start back up. The documentation in http://developer.android.com/reference/android/app/Activity.html has a good description. – jjb Jun 20 '12 at 06:00
4

I can not using the data. But I think you need changed as follow

photo to become general variable.

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

So, you must be take it from this folder:

Bitmap photo = Media.getBitmap(getContentResolver(), Uri.fromFile(photo) );

I follow your code and change.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Song Vo
  • 41
  • 2
  • EXTRA_OUTPUT and get("data") are mutually exclusive, per offical doc for [MediaStore.ACTION_IMAGE_CAPTURE](https://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE): *If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field* – Alex Cohn Dec 28 '17 at 11:59
1

I struggled with this problem and found another solution: just use code like this:

Bitmap bitmap = data.getExtras().getParcelable("data");

and you can get the bitmap.

Antzi
  • 12,831
  • 7
  • 48
  • 74
Jaden Gu
  • 9,143
  • 3
  • 23
  • 26
1

Basically put, using

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

works well

Before placing in an imageView, make sure that you compress the image since it is extremely large when saving this way.

int newWidth = bm.getWidth();
int newHeight = bm.getHeight();
while(newWidth > 300){
    newWidth = newWidth/2;
newHeight = newHeight/2;
}
mImagePlaceHolder.setImageBitmap(Bitmap.createScaledBitmap(bm, newWidth, newHeight, false));
Droid Chris
  • 3,455
  • 28
  • 31