Android 如何获取画廊图像的压缩图像uri?
我在工作的项目中,有一个需要压缩的图像,并需要得到压缩图像的Uri。问题是,当我试图从gallery压缩图像时,它正在创建一个以上的图像,该图像与具有不同分辨率的原始图像一起被压缩。如何实现这样的效果:我应该在gallery中只有一个图像引用,也是原始图像,而不是压缩图像。请在下面找到我尝试过的代码片段Android 如何获取画廊图像的压缩图像uri?,android,bitmap,android-gallery,image-compression,Android,Bitmap,Android Gallery,Image Compression,我在工作的项目中,有一个需要压缩的图像,并需要得到压缩图像的Uri。问题是,当我试图从gallery压缩图像时,它正在创建一个以上的图像,该图像与具有不同分辨率的原始图像一起被压缩。如何实现这样的效果:我应该在gallery中只有一个图像引用,也是原始图像,而不是压缩图像。请在下面找到我尝试过的代码片段 public static Bitmap getResizedBitmap(Bitmap image, int maxSize) { int width = image.get
public static Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
private static Bitmap rotateImage(Bitmap img, float degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
//img.recycle();
return rotatedImg;
}
private static Bitmap rotateImageIfRequired(Context context, Bitmap img, Uri selectedImage) throws IOException {
InputStream input = context.getContentResolver().openInputStream(selectedImage);
ExifInterface ei;
if (Build.VERSION.SDK_INT > 23)
ei = new ExifInterface(input);
else
ei = new ExifInterface(selectedImage.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotateImage(img, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotateImage(img, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotateImage(img, 270);
default:
return img;
}
}
public static Uri compressedImageUri(Uri selectedImage, Context context) {
try {
InputStream imageStream = null;
try {
imageStream = context.getContentResolver().openInputStream(
selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap original = BitmapFactory.decodeStream(imageStream);
//Bitmap bmp = getResizedBitmap(original, 500);
Bitmap rotateBitmap = rotateImageIfRequired(context, original, selectedImage);
Bitmap bmp = getResizedBitmap(rotateBitmap, 500);
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bmp, String.valueOf(System.currentTimeMillis()), null);
return Uri.parse(path);
} catch (Exception e) {
Log.d("error", e.getLocalizedMessage());
}
return null;
}
用这个
您可能需要其他课程,所以请检查套餐。
希望这能对你有所帮助。使用这个
您可能需要其他课程,所以请检查套餐。
希望这会对您有所帮助。不要使用
insert()
,而是在“selectedImage”上打开一个输出流,并将位图压缩到其中。库中的一个图像引用也是原始的。
?也?你是说仅仅是?如果你有两个图像,你有两个网址。如果你不想保留原版,那么使用我的代码。如果这不适合您,则不清楚您想要什么。不要使用insert()
,而是在“selectedImage”上打开一个输出流,并将位图压缩到其中。库中的一个图像引用也是原始的
?也?你是说仅仅是?如果你有两个图像,你有两个网址。如果你不想保留原版,那么使用我的代码。如果这不适合您,则不清楚您想要什么。thisInCompressor
类的压缩比是多少,具有固定的高度和宽度,但您可以根据自己的要求进行修改。压缩比将与实际图像相同thisInCompressor
类的压缩比是多少,具有固定的高度和宽度宽度,但您可以根据需要修改。压缩比将与实际图像相同
File imgFile = new File(uri.getPath());
File compressFile = Compressor.getDefault(getContext()).compressToFile(imgFile);