Android:获取我们在图库中看到的图像而不是 Stratch 图像

发布于 2025-01-02 10:43:43 字数 2561 浏览 1 评论 0原文

在我的应用程序中,我使用此代码从图库和相机获取图像:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {     
    super.onActivityResult(requestCode, resultCode, data);     
    if (requestCode == 10 && resultCode == Activity.RESULT_OK) {             
        Uri contentUri = data.getData();          
        String[] proj = { MediaStore.Images.Media.DATA };         
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);         
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);         
        cursor.moveToFirst();         
        imagePath = cursor.getString(column_index);           
        //Bitmap croppedImage = BitmapFactory.decodeFile(imagePath);
        tempBitmap = BitmapFactory.decodeFile(imagePath);
        if(!(tempBitmap == null))
        {
            // here i am changing display to the tempBitmap
            photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);
            //photoBitmap = Bitmap.createBitmap(tempBitmap, 0, 0, tempBitmap.getWidth(), tempBitmap.getHeight());
            takePhotoFromGallery = true;// edited
        }
        else
            Toast.makeText(getApplicationContext(), "Image is not valid", Toast.LENGTH_SHORT).show();

    } 
    if(resultCode == RESULT_OK && requestCode==TAKE_PHOTO_CODE){
         final File file = getTempFile(this);         
         try {           
             tempBitmap = Media.getBitmap(getContentResolver(), Uri.fromFile(file));
             photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);
             takePhotoFromCamera = true;
             // do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)         
        } catch (FileNotFoundException e) {           
            e.printStackTrace();         
        } catch (IOException e) {           
            e.printStackTrace();         
        } 
    }
}

现在,一切正常。但我得到的图像是Stratch。

我认为这是因为这一行:

photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);

这里我用显示器的宽度、高度来拉伸图像的宽度、高度。

我想要的是图像应该是 Dispaly,因为我们通常可以在图库中显示图像。那么如何才能实现呢???

这是我从相机拍摄的图像:

在此处输入图像描述

现在这是我在应用程序中看到的内容:

在此处输入图像描述

现在第二张图像几乎没有了。因为那行代码。

那么我应该怎么做才能使其高度和宽度正常? 谢谢。 谢谢。

In my app i am using this code to get Image from Gallery and Camera:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {     
    super.onActivityResult(requestCode, resultCode, data);     
    if (requestCode == 10 && resultCode == Activity.RESULT_OK) {             
        Uri contentUri = data.getData();          
        String[] proj = { MediaStore.Images.Media.DATA };         
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);         
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);         
        cursor.moveToFirst();         
        imagePath = cursor.getString(column_index);           
        //Bitmap croppedImage = BitmapFactory.decodeFile(imagePath);
        tempBitmap = BitmapFactory.decodeFile(imagePath);
        if(!(tempBitmap == null))
        {
            // here i am changing display to the tempBitmap
            photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);
            //photoBitmap = Bitmap.createBitmap(tempBitmap, 0, 0, tempBitmap.getWidth(), tempBitmap.getHeight());
            takePhotoFromGallery = true;// edited
        }
        else
            Toast.makeText(getApplicationContext(), "Image is not valid", Toast.LENGTH_SHORT).show();

    } 
    if(resultCode == RESULT_OK && requestCode==TAKE_PHOTO_CODE){
         final File file = getTempFile(this);         
         try {           
             tempBitmap = Media.getBitmap(getContentResolver(), Uri.fromFile(file));
             photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);
             takePhotoFromCamera = true;
             // do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)         
        } catch (FileNotFoundException e) {           
            e.printStackTrace();         
        } catch (IOException e) {           
            e.printStackTrace();         
        } 
    }
}

Now, all works fine. But i got Image as Stratch.

I thaught it is because this line:

photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);

here i am stratching the image width,height with display's width,height.

I want is the Image should be Dispaly as we can normaly show thew image in gallery. So how to make it possible ???

This is the image which i am snaping from the Camera:

enter image description here

And Now this is What i see in my Application:

enter image description here

Now Second image got little stratch. as because of that line code.

So what should i have to do to make is normal in its height and width ??
Thanks.
Thanks.

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

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

发布评论

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

评论(1

南笙 2025-01-09 10:43:43

使用 ImageView 并将图像视图的缩放类型设置为 Center-Inside。

Use ImageView and set scale type of image view to Center-Inside.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文