简体   繁体   English

android-从图库/相机拍照的ANR

[英]android - ANR on taking picture from gallery/camera

Did someone face the issue when ANR happened after choosing picture from gallery or taking a photo from camera (in my case it returns byte[] array to my app)? 当从图库中选择图片或从相机拍摄照片后发生ANR时,有人遇到过问题吗(在我的情况下,它向我的应用返回了byte []数组)?

How to solve it? 怎么解决呢?

Upd. 更新。 Here's the code how an image is picked. 这是如何选择图像的代码。 But this part of code didn't change and some time ago it worked (some other parts of code were changed, I don't know if it's important). 但是这部分代码没有更改,并且在一段时间之前可以工作(其他部分代码已更改,我不知道它是否重要)。

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

For the case of SELECT_FILE , you can try getting Bitmap like below. 对于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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM