not able to downsize the image

北城余情 提交于 2019-12-11 23:22:14

问题


I have made an app in which user select image from gallery.But when user select an image that is taken from camera i get "Memory OutOFBound error".I tried downsizing it but no sucess.Please help me.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult (requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK && null != data) {


            selectedImage = data.getData ();
            try {
                decodeUri (getApplicationContext (),selectedImage,100);
            } catch (FileNotFoundException e) {
                e.printStackTrace ();
            }
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver ().query (selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst ();

            int columnIndex = cursor.getColumnIndex (filePathColumn[0]);
            picturePath = cursor.getString (columnIndex);
            drawable = Drawable.createFromPath (picturePath);



            cursor.close ();
            dialog = new Dialog (CaptureImage.this,android.R.style.Theme_DeviceDefault);
            dialog.requestWindowFeature (Window.FEATURE_NO_TITLE);
            dialog.setContentView (R.layout.confirmation_image);
            dialog.getWindow ().setLayout (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            ImageView image = (ImageView) dialog.findViewById (R.id.iv_selected_image);
            image.setImageDrawable (drawable);
            ok_gallery = (Button) dialog.findViewById (R.id.bt_ok_gallery);
            cancel = (Button) dialog.findViewById (R.id.bt_cancel);
            ok_gallery.setOnClickListener (this);
            cancel.setOnClickListener (this);
            dialog.show (); 

decodeUri Code

public static Bitmap decodeUri(Context c, Uri uri, final int requiredSize)
            throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o);

        int width_tmp = o.outWidth
                , height_tmp = o.outHeight;
        int scale = 1;

        while(true) {
            if(width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2);
    }

来源:https://stackoverflow.com/questions/23911124/not-able-to-downsize-the-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!