微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 ImageView 中显示压缩图像并显示其大小?

如何解决如何在 ImageView 中显示压缩图像并显示其大小?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                ContentResolver resolver = getContentResolver();
                ContentValues contentValues = new ContentValues();
                contentValues.put(MediaStore.MediaColumns.disPLAY_NAME,"Image" + timestamp + ".jpg");
                contentValues.put(MediaStore.MediaColumns.MIME_TYPE,"image/*");
                contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH,Environment.DIRECTORY_PICTURES + File.separator + "Compressed_Images");
                imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
    
            

fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
bitmap.compress(WEBP_LOSSY,10,fos);
            Objects.requireNonNull(fos);

在图像视图中显示------>

 InputStream is = getContentResolver().openInputStream(imageUri);
                Bitmap bitmap1 = BitmapFactory.decodeStream(is);
                compImage.setimageBitmap(bitmap1);

它的压缩和保存图像没有问题

解决方法

在您的 Activity 的 xml 布局中添加一个 ImageView。像这样:

<ImageView
     android:id="@+id/my_image_view"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />

然后在您的 Activity 的 create 方法中,您将获得对该 ImageView 的引用。像这样:

ImageView myImageView = findViewById(R.id.my_image_view);

现在你用你的位图作为参数在你的 imageView 上调用“setImageBitmap”。像这样:

myImageView.setImageBitmap(bitmap); // "bitmap" is your bitmap

如果我理解正确的话,这应该就是你想做的

,
 InputStream is = getContentResolver().openInputStream(imageUri);
            Bitmap bitmap1 = BitmapFactory.decodeStream(is);
            compImage.setImageBitmap(bitmap1);

好的....我设法在 Imageview 中设置图像(我希望它的压缩图像) 现在我如何获得这个的大小?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。