Android:从相机获取图片时发生NPE

我的Android应用有问题。 我使用MediaStore.ACTION_IMAGE_CAPTURE意图从相机拍摄照片。 它适用于大多数设备,但是我从Android市场获得了DroidX设备上的以下故障错误。

在这种情况下,我认为imageCaptureUri不能为null。 所以这不是原因。

你有什么想法? 这是一个固件错误?

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


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

这是我的实现:

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();

        ...
    }
}

你确定图像裁剪模式强制

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

适用于您使用的设备。 纠正我,如果我错了,但它是不安全的,没有文件记录的方法。 在这里你可以找到没有裁剪的工作代码的例子。

更新:你面临的问题有很长的历史,也在SO:

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

我遇到的问题是在相机拍摄图像后立即使用作物。 此外,它不兼容通过不同的Android版本,所以如果你得到1.5的工作,也许它不适用于2.3。 肯定有些事情是错误的,正如可以从Android开发人员组发布的帖子中得出的结论:

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

链接地址: http://www.djcxy.com/p/32019.html

上一篇: Android: NPE happens when getting picture from camera

下一篇: Android: use a datepicker and timepicker from within a Dialog