gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:39 24 4
gpt4 key购买 nike

我的 Android 应用程序有问题。我正在使用 MediaStore.ACTION_IMAGE_CAPTURE Intent 从相机拍照。它适用于所有大多数设备,但我在 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();

...
}
}

最佳答案

你确定图像裁剪模式强制

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

适用于您使用的设备。如果我错了请纠正我,但这是不安全且没有详细记录的方法。在这里您可以找到 working code 的示例没有裁剪。

更新:您面临的问题由来已久,也在 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

关于Android:从相机获取图片时发生 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4264924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com