android - ANR从画廊/相机拍照

时间:2016-06-10 08:40:24

标签: android android-camera gallery android-anr-dialog

在从图库中选择图片或从相机拍摄照片(在我的情况下,它将byte []数组返回到我的应用程序)时,有人遇到问题吗?

如何解决?

更新。以下是搜索图像的代码。但是这段代码没有改变,前段时间它有用(代码的其他部分已经改变,我不知道它是否重要)。

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;
    }
}

1 个答案:

答案 0 :(得分:0)

对于SELECT_FILE的情况,您可以尝试获取Bitmap,如下所示。

 case SELECT_FILE:
            if (data != null) {
                try {
                    Uri selectedImage = data.getData();
                    String[] filePath = {MediaStore.Images.Media.DATA};
                    Cursor c = getApplicationContext().getContentResolver().query(
                            selectedImage, filePath, null, null, null);
                    c.moveToFirst();
                    int columnIndex = c.getColumnIndex(filePath[0]);
                    String picturePath = c.getString(columnIndex);
                    c.close();
                    Bitmap thumbnail = BitmapFactory.decodeFile(picturePath); 

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;