在缩放尺寸的图像上显示图像从图库中查看

发布于 2024-12-19 08:45:58 字数 1072 浏览 3 评论 0原文

我想在图库中的 imageView 上显示图像。 当我从图库中调用图片时,我想将图像的大小固定为来自相机的图像。 你能把手伸给我吗? 当我从相机中选取时,它可以将图像缩放为createScaledBitmap。 如果您有除 setImageURI 之外的其他想法,您能给我建议吗? 我可以使用 setImageBitmap 代替 URI 吗? 我想将这些图片与相关列表一起保存,例如保存到 blob 类型的 sqlite 中。 请给我建议。谢谢。

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
     if (resultCode != RESULT_OK) return;
       switch (requestCode)
       {
        case PICK_FROM_CAMERA:
            Bundle extras = data.getExtras();
            Bitmap selectedImage = (Bitmap) extras.get("data");
            selectedImage = Bitmap.createScaledBitmap(selectedImage, 200, 250, false);
            mImageView.setImageBitmap(selectedImage);
            break;

        case PICK_FROM_GALLERY:
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            mImageView.setImageURI(selectedImageUri);
        break;
       }
}

I want to display iamge on imageView from gallery.
When I call picture from gallery, I want to fix size the image as from camera.
Could you give me some your hand?
When I pick from camera, it can be scaled image as createScaledBitmap.
If you have other idea not setImageURI, could you give me advice?
Can I use setImageBitmap instead of URI?
I want to save these picture with related list such as saving into sqlite for blob type.
Please advice me. Thanks.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
     if (resultCode != RESULT_OK) return;
       switch (requestCode)
       {
        case PICK_FROM_CAMERA:
            Bundle extras = data.getExtras();
            Bitmap selectedImage = (Bitmap) extras.get("data");
            selectedImage = Bitmap.createScaledBitmap(selectedImage, 200, 250, false);
            mImageView.setImageBitmap(selectedImage);
            break;

        case PICK_FROM_GALLERY:
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            mImageView.setImageURI(selectedImageUri);
        break;
       }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

走过海棠暮 2024-12-26 08:45:58

在 CASE PICK_FROM_GALLERY 中尝试此操作

Uri selectedImageUri = data.getData();
   selectedImagePath = getPath(selectedImageUri);

Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);

  Bitmap bt=Bitmap.createScaledBitmap(bitmap, 150, 150, false);  

  photo_image.setImageBitmap(bt)

try this in CASE PICK_FROM_GALLERY

Uri selectedImageUri = data.getData();
   selectedImagePath = getPath(selectedImageUri);

Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);

  Bitmap bt=Bitmap.createScaledBitmap(bitmap, 150, 150, false);  

  photo_image.setImageBitmap(bt)
潜移默化 2024-12-26 08:45:58
Bitmap photobitmap = BitmapFactory.decodeFile(selectedImagePath);


if(photoBitmap!=null)
                            {   
                                // Compressing large image to small one 

                                Display display = getWindowManager().getDefaultDisplay(); 

                                int width = display.getWidth();
                                int height = display.getHeight();


                             int photo_width = photoBitmap.getWidth();
                             int photo_height = photoBitmap.getHeight();

                            if(photo_width >width)
                                photo_width = width;

                           if(photo_height > height)
                                photo_height = height;

                                    photoBitmap = Bitmap.createScaledBitmap (photoBitmap, photo_width , photo_height , false);

                                    photo.setImageBitmap(photoBitmap);
                             }

// 如果你的 imageView 高度和宽度是固定的,请指定它们。为了更好地理解,我记录了设备的高度和宽度。

Bitmap photobitmap = BitmapFactory.decodeFile(selectedImagePath);


if(photoBitmap!=null)
                            {   
                                // Compressing large image to small one 

                                Display display = getWindowManager().getDefaultDisplay(); 

                                int width = display.getWidth();
                                int height = display.getHeight();


                             int photo_width = photoBitmap.getWidth();
                             int photo_height = photoBitmap.getHeight();

                            if(photo_width >width)
                                photo_width = width;

                           if(photo_height > height)
                                photo_height = height;

                                    photoBitmap = Bitmap.createScaledBitmap (photoBitmap, photo_width , photo_height , false);

                                    photo.setImageBitmap(photoBitmap);
                             }

// If your imageView height and widht are fixed specify them. I have taken device height and width for better understanding.

情定在深秋 2024-12-26 08:45:58
ImageView imageView = findViewById(R.id.photo_image);

Bitmap photobitmap = BitmapFactory.decodeFile(selectedImagePath);


if(photoBitmap!=null)
                            {   

                             int photo_width = photoBitmap.getWidth();
                             int photo_height = photoBitmap.getHeight();
   //1dp = 1.5 px 150dp = 225px 

                            if(photo_width >225)
                                photo_width = 225;

                           if(photo_height > 225)
                                photo_height = 225;

                                    photoBitmap = Bitmap.createScaledBitmap (photoBitmap, photo_width , photo_height , false);

                                    photo.setImageBitmap(photoBitmap);
                             }
ImageView imageView = findViewById(R.id.photo_image);

Bitmap photobitmap = BitmapFactory.decodeFile(selectedImagePath);


if(photoBitmap!=null)
                            {   

                             int photo_width = photoBitmap.getWidth();
                             int photo_height = photoBitmap.getHeight();
   //1dp = 1.5 px 150dp = 225px 

                            if(photo_width >225)
                                photo_width = 225;

                           if(photo_height > 225)
                                photo_height = 225;

                                    photoBitmap = Bitmap.createScaledBitmap (photoBitmap, photo_width , photo_height , false);

                                    photo.setImageBitmap(photoBitmap);
                             }
dawn曙光 2024-12-26 08:45:58

您可以使用位图工厂选项 &如果需要,可以调整图像的比例..

Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8; // your sample size 
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath,options);  
mImageView.setImageBitmap(bitmap);

You can use Bitmap factory options & resize the scale of the image if you want ..

Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8; // your sample size 
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath,options);  
mImageView.setImageBitmap(bitmap);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文