处理相机意图的内存不足错误imageView预览

时间:2016-09-09 09:44:22

标签: android android-intent

我正在开发一个Android应用程序,它必须拍照并在上传到服务器之前显示预览。 (听起来很简单,对吧?至少我认为如此)。 我认为我的问题是内存不足错误,因为有时它会设法显示图片,但大多数时候应用程序才会关闭。

以下是如何使用意图:

  imageCaptureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
            Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            stamp = accNum[pos] + "_" + s.format(new Date());
            File f = new File(Environment.getExternalStorageDirectory(), stamp + ".jpg");
            chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            imageToUploadUri = Uri.fromFile(f);
            imagePath = f.getPath();
            stopFusedLocation();
            startActivityForResult(chooserIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);


        }
    });

以下是我如何解码捕获的图像

 private Bitmap getBitmap(String path) {

    Uri uri = Uri.fromFile(new File(path));
    InputStream in = null;
    try {
        final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
        in = getActivity().getContentResolver().openInputStream(uri);

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, o);
        in.close();


        int scale = 1;
        while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) >
                IMAGE_MAX_SIZE) {
            scale++;
        }
        Log.d("", "scale = " + scale + ", orig-width: " + o.outWidth + ", orig-height: " + o.outHeight);

        Bitmap b = null;
        in = getActivity().getContentResolver().openInputStream(uri);
        if (scale > 1) {
            scale--;
            // scale to max possible inSampleSize that still yields an image
            // larger than target
            o = new BitmapFactory.Options();
            o.inSampleSize = scale;
            b = BitmapFactory.decodeStream(in, null, o);

            // resize to desired dimensions
            int height = b.getHeight();
            int width = b.getWidth();
            Log.d("", "1th scale operation dimenions - width: " + width + ", height: " + height);

            double y = Math.sqrt(IMAGE_MAX_SIZE
                    / (((double) width) / height));
            double x = (y / height) * width;

            Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
                    (int) y, true);
            b.recycle();
            b = scaledBitmap;

            System.gc();
        } else {
            b = BitmapFactory.decodeStream(in);
        }
        in.close();

        Log.d("", "bitmap size - width: " + b.getWidth() + ", height: " +
                b.getHeight());
        return b;
    } catch (IOException e) {
        Log.e("", e.getMessage(), e);
        return null;
    }
}

以下是我处理活动结果的方法

 /**
 * Receiving activity result method will be called after closing the camera
 * */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        if(imageToUploadUri != null){
            Uri selectedImage = imageToUploadUri;
            getActivity().getContentResolver().notifyChange(selectedImage, null);
            Bitmap reducedSizeBitmap = getBitmap(imageToUploadUri.getPath());
            if(reducedSizeBitmap != null){

                bkl.setImageBitmap(reducedSizeBitmap);




            }else{
                Toast.makeText(getContext(),"Error while capturing Image",Toast.LENGTH_LONG).show();
            }
        }else{
            Toast.makeText(getActivity(),"Error while capturing Image",Toast.LENGTH_LONG).show();
        }
    }
}

请不要说它是一个重复的问题,因为我已经阅读了有关此问题的所有其他问题,似乎没有任何效果。我甚至在清单中尝试android:largeHeap = true,然后恢复实例状态,一切,似乎没有任何效果。

任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

检查LeakCanary,看看你的活动在哪里泄漏。

你也喜欢意大利面条代码:

OK

这是没办法的。创建上下文或活动并修复所有内容。

答案 1 :(得分:0)

如果通过imageToUploadUri传递intent并不重要,那么您可以使用intent(Bitmap) intent.getExtras().get("data")的附加内容中获取缩略图。否则,请检查imageToUploadUri.getPath()是否返回正确的路径。您可以使用以下方法从uri获取该图像的路径。

public String getPathFromURI(Uri uri){
    String filePath = "";
    String[] filePahColumn = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(uri, filePahColumn, null, null, null);
    if (cursor != null) {
        if(cursor.moveToFirst()){
            int columnIndex = cursor.getColumnIndex(filePahColumn[0]);
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();
    }
    return filePath;
}

愿这有帮助。