如何在 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 中设置图像(我希望它的压缩图像) 现在我如何获得这个的大小?