【发布时间】: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