Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: NPE happens when getting picture from camera

Tags:

android

camera

I have a problem on my Android app. I'm using MediaStore.ACTION_IMAGE_CAPTURE intent to take picture from camera. It worked all most of devices but I got the following crash errors on DroidX device from android market.

I think imageCaptureUri can not be null in this case. So it is not the cause.

Do you have any ideas? Is it a firmware error?

java.lang.NullPointerException at android.content.ContentResolver.openInputStream(ContentResolver.java:286) at com.eb.android.activity.AddActivity.getBase64Receipt(AddActivity.java:193) at com.eb.android.activity.AddActivity.publishReceipt(AddActivity.java:204) at com.eb.android.activity.AddActivity.access$0(AddActivity.java:203) at com.eb.android.activity.AddActivity$1.run(AddActivity.java:50) at java.lang.Thread.run(Thread.java:1102)


java.lang.NullPointerException at android.content.ContentResolver.openInputStream(ContentResolver.java:288) at com.eb.android.activity.AddActivity.getBase64Receipt(AddActivity.java:193) at com.eb.android.activity.AddActivity.publishReceipt(AddActivity.java:204) at com.eb.android.activity.AddActivity.access$0(AddActivity.java:203) at com.eb.android.activity.AddActivity$1.run(AddActivity.java:50) at java.lang.Thread.run(Thread.java:1096)

This is my implementation:

public class AddActivity extends Activity {

    public static final int TAKE_RECEIPT = 2;

    private Uri imageCaptureUri;

    private Runnable submitReceiptRunnable = new Runnable() {
        public void run() {
            publishReceipt();
        }
    };

    private ProgressDialog progressDialog;

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

        registerListeners();
    }

    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
            case (TAKE_RECEIPT):
                takeReceiptCallback(resultCode, data);
                break;
        }
    }

    private void registerListeners() {
        ImageView addReceiptButton = (ImageView) findViewById(R.id.AddReceiptButton);
        addReceiptButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                takePictureFromCamera();
            }
        });
    }

    private void takePictureFromCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        imageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "tmp_receipt_"
                + String.valueOf(System.currentTimeMillis()) + ".jpg"));

        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageCaptureUri);
        intent.putExtra("return-data", true);

        startActivityForResult(intent, TAKE_RECEIPT);
    }

    private void takeReceiptCallback(int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            submitReceipt();
        }
    }

    private void submitReceipt() {
        Thread thread = new Thread(null, submitReceiptRunnable);
        thread.start();
        progressDialog = ProgressDialog.show(this, "Please wait...", "Publishing receipt ...", true);
    }

    private String getBase64Receipt() {
        try {
            InputStream inputStream = getContentResolver().openInputStream(imageCaptureUri);
            byte[] bytes = CommonUtil.getBytesFromInputStream(inputStream);
            return Base64.encodeBytes(bytes);
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
        }

        return null;
    }

    private void publishReceipt() {
        String receipt = getBase64Receipt();

        ...
    }
}
like image 330
user423715 Avatar asked Nov 24 '10 08:11

user423715


1 Answers

Are you sure that image crop mode forcing

intent.putExtra("return-data", true); 

works correctly for the device you use. Correct me if I am wrong, but it is not safe and not well documented approach. Here you can find example of working code without cropping.

UPDATE: Issue you are facing with has long history, also at SO:

https://stackoverflow.com/questions/3904685/unable-to-find-com-android-camera-cropimage-activity-in-android

Issue I experienced was using crop immediatelly after image was taken by the camera. Also, it is not compatible through different Android versions, so if you get it working for 1.5 maybe it will not work for 2.3. Definitely something is wrong, as may be concluded from the Android Developer Group posts:

http://groups.google.com/group/android-developers/browse_thread/thread/2dd647523926192c/569f36b5b28f2661?lnk=gst&q=Crop+image+intent#569f36b5b28f2661

http://groups.google.com/group/android-developers/browse_thread/thread/2dd647523926192c/dcbe5aef29eddad6?lnk=gst&q=Crop+image+intent#dcbe5aef29eddad6

http://groups.google.com/group/android-developers/browse_thread/thread/d7b6a133c164aa17/184bf3b85da2ce58?lnk=gst&q=Crop+image+intent#184bf3b85da2ce58

like image 108
Zelimir Avatar answered Nov 15 '22 22:11

Zelimir