Android:从相机获取图片时发生NPE
我的 Android 应用程序出现问题。我正在使用 MediaStore.ACTION_IMAGE_CAPTURE
意图从相机拍照。它适用于大多数设备,但我在 Android 市场的 DroidX 设备上遇到以下崩溃错误。
我认为在这种情况下 imageCaptureUri
不能为空。所以这不是原因。
你有什么想法吗?是固件错误吗?
java.lang.NullPointerException 位于 android.content.ContentResolver.openInputStream(ContentResolver.java:286) 在 com.eb.android.activity.AddActivity.getBase64Receipt(AddActivity.java:193) 在 com.eb.android.activity.AddActivity.publishReceipt(AddActivity.java:204) 在 com.eb.android.activity.AddActivity.access$0(AddActivity.java:203) 在 com.eb.android.activity.AddActivity$1.run(AddActivity.java:50) 在 java.lang.Thread.run(Thread.java:1102)
<小时>java.lang.NullPointerException 位于 android.content.ContentResolver.openInputStream(ContentResolver.java:288) 在 com.eb.android.activity.AddActivity.getBase64Receipt(AddActivity.java:193) 在 com.eb.android.activity.AddActivity.publishReceipt(AddActivity.java:204) 在 com.eb.android.activity.AddActivity.access$0(AddActivity.java:203) 在 com.eb.android.activity.AddActivity$1.run(AddActivity.java:50) 在 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();
...
}
}
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();
...
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定图像裁剪模式强制
适用于您使用的设备吗?如果我错了,请纠正我,但这不是安全且没有详细记录的方法。在这里您可以找到工作代码 无需裁剪。
更新:您面临的问题有着悠久的历史,也在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
Are you sure that image crop mode forcing
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