【问题标题】:How do I display the Compressed image in ImageView and display its Size?如何在 ImageView 中显示压缩图像并显示其大小?
【发布时间】:2021-03-26 18:51:44
【问题描述】:
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);

让它显示在 imageview 中------>

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

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

【问题讨论】:

  • 如果您已经有了位图,您应该能够在图像视图上调用“setImageBitmap”并设置位图。您可以在 textview 中显示大小,方法是在位图上调用 getWidth 和 getHeight,用它们制作一个字符串,然后将此字符串设置为 TextView 的文本。
  • 我理解逻辑......但我有 bitmap.compress(WEBP_LOSSY, 10, fos);这是位图我如何在ImageView中设置它......你能在代码中帮助我吗
  • 您的 xml 代码中是否已有图像视图?您需要调用 findViewById 来获取对 imageview 的引用,然后在 imageview 上调用“setImageBitmap”,并将您的位图作为参数
  • A 在下方添加了一个带有代码示例的答案
  • 是的,我的 xml 中有 imageView

标签: java android android-imageview image-compression


【解决方案1】:

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

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

然后在您的活动的创建方法中,您将获得对该 ImageView 的引用。像这样:

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

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

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

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

【讨论】:

  • 好的...我认为您在“bitmap”和“bitmap.compress(WEBP_LOSSY, 10, fos)”之间搞错了......无法在 imageview 中设置 bitmap.compress跨度>
  • 但是您已经有了位图。因为您正在调用 compress 。所以它已经加载到 RAM 中了。那么为什么不使用这个已经加载的图像将其设置为图像视图。也许我误解了你。但是你的位图对象不是数据类型 Bitmap 吗?
  • 或者您想先压缩图像,将其保存到输出流,然后显示压缩后的图像?哦。
  • 您要做的是在解析器上打开一个输入流,然后使用 BitmapFactory.decodeStream(InputStream is) 将此流作为新位图读取
  • 是的......我绝对想这样做..谢谢你的快速反应..好的怎么做它只是举个例子..
【解决方案2】:
 InputStream is = getContentResolver().openInputStream(imageUri);
            Bitmap bitmap1 = BitmapFactory.decodeStream(is);
            compImage.setImageBitmap(bitmap1);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-09
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多