从画廊中选择图片或从相机拍摄照片(在我的情况下,它返回byte []数组到我的应用程序)后,有人面临的问题?android-ANR从画廊/相机拍摄图片
如何解决?
Upd。这是代码如何挑选图像的代码。但是这部分代码没有改变,前一段时间它的工作(代码的其他部分发生了变化,我不知道它是否重要)。
void chooseOrTakePhotoDialog(OnActivityResultListener listener) {
onActivityResultListener = listener;
final CharSequence[] items = { "Take Photo", "Choose from Library", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add Photo");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
interface OnActivityResultListener {
void onPhotoByteArray(byte[] bytes);
}
private OnActivityResultListener onActivityResultListener;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.wtf("onActivityResult", "req="+requestCode+" res="+resultCode+" data="+data);
if (resultCode != RESULT_OK) return;
switch (requestCode) {
case REQUEST_CAMERA:
if (onActivityResultListener != null) {
Bitmap bmp = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
onActivityResultListener.onPhotoByteArray(stream.toByteArray());
}
break;
case SELECT_FILE:
Uri uri = data.getData();
try {
byte[] bytes = Helper.readBytes(getContentResolver().openInputStream(uri));
onActivityResultListener.onPhotoByteArray(bytes);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
请发布您的代码和错误。 – earthw0rmjim
它很奇怪,但在Logs中没有错误...它只是冻结然后关闭(但前一段时间它的工作,与选择相同的代码;我改变了一些其他的东西) –
你为什么要返回字节数组?我可以知道原因吗? – SripadRaj