我的Android应用程序出现了问题。我正在使用MediaStore.ACTION_IMAGE_CAPTURE
意图从相机拍照。它在大多数设备上都可以工作,但是在来自Android市场的DroidX设备上出现了以下崩溃错误。
我认为在这种情况下imageCaptureUri
不能为null。因此,这不是原因。
你有什么想法吗?这是固件错误吗?
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)
这是我的实现:
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();
...
}
}