如何调整图像我从画廊采摘机器人? [英] How to resize an image i picked from the gallery in android?

查看:121
本文介绍了如何调整图像我从画廊采摘机器人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个机器人在哪里。内的一个活动,我有一个图像按钮。当我点击它的画廊打开了,我可以选择图像。然后,我设置的图像作为图像按钮的新形象。 现在的问题是图像出现在我的活动方式太大。我怎样才能使它适合我的形象按钮?

 保护无效onActivityResult(INT申请code,INT结果code,意图imageReturnedIntent){
    super.onActivityResult(要求code,因此code,imageReturnedIntent);

    开关(要求code){
    案例SELECT_PHOTO:
        如果(结果code == RESULT_OK){
            乌里selectedImage = imageReturnedIntent.getData();
            的String [] filePathColumn = {MediaStore.Images.Media.DATA};

            光标光标= getContentResolver()查询(selectedImage,filePathColumn,NULL,NULL,NULL);
            cursor.moveToFirst();

            INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
            字符串文件路径= cursor.getString(参数:columnIndex);
            cursor.close();


            位图yourSelectedImage = BitmapFactory.de codeFILE(文件路径);

            mImageButton.setImageBitmap(yourSelectedImage);
        }
    }
}
 

解决方案

您可以使用此方法来获得调整后的图像。这样你就可以避免的OutOfMemoryError

 公共静态位图德codeURI(上下文C,开放的URI,最终诠释requiredSize)
            抛出FileNotFoundException异常{
        BitmapFactory.Options O =新BitmapFactory.Options();
        o.inJustDe codeBounds = TRUE;
        BitmapFactory.de codeStream(c.getContentResolver()openInputStream(URI),空,邻。);

        INT width_tmp = o.outWidth
                ,height_tmp = o.outHeight;
        int标= 1;

        而(真){
            如果(width_tmp / 2'; requiredSize || height_tmp / 2'; requiredSize)
                打破;
            width_tmp / = 2;
            height_tmp / = 2;
            规模* = 2;
        }

        BitmapFactory.Options O2 =新BitmapFactory.Options();
        o2.inSampleSize =规模;
        返回BitmapFactory.de codeStream(c.getContentResolver()openInputStream(URI),空,O2);
    }
 

I am building an android where. Inside of one activity I have an image button. When I click on it the gallery opens up and I can choose an image. Then I set that image as the new image for the image button. The problem is the image appears way too big inside my activity. How can I make it fit into my image button?

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

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

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();


            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);

            mImageButton.setImageBitmap(yourSelectedImage);
        }
    }
}

解决方案

You can use this method to get a resized image. This way you can avoid OutOfMemoryError

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

这篇关于如何调整图像我从画廊采摘机器人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆