上传图片时如何压缩文件大小和质量?
How can I compress the file size and quality when uploading pictures?
上传图片时如何压缩文件大小和质量?
我已经上传文件到firebase,如何压缩文件?
因为当文件很大时,recyclerview 加载会很慢。
所以我想压缩文件。
Uri imageUri = imageListUri.get(index);
if(spinner.getSelectedItem().toString().equals(getApplicationContext().getResources().getString(R.string.choose_sub))) {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle(getApplicationContext().getResources().getString(R.string.alert_error))
.setMessage(getApplicationContext().getResources().getString(R.string.choose_sub))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
alertDialog.show();
} else if(image_check.equals("ok")) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage(getApplicationContext().getResources().getString(R.string.posting));
progressDialog.show();
if (imageUri != null) {
final StorageReference filerefrence = storageReference.child(System.currentTimeMillis()
+ "." + getFileExtension(imageUri));
// scaling the image
int scaleDivider = 1;
try {
Bitmap fullBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
int scaleWidth = fullBitmap.getWidth() / scaleDivider;
int scaleHeight = fullBitmap.getHeight() / scaleDivider;
byte[] downsizedImageBytes =
getDownsizedImageBytes(fullBitmap, scaleWidth, scaleHeight);
uploadTask = filerefrence.putBytes(downsizedImageBytes);
} catch (IOException e) {
e.printStackTrace();
}
static public byte[] getDownsizedImageBytes(Bitmap fullBitmap, int scaleWidth, int scaleHeight) throws IOException {
Bitmap scaledBitmap = Bitmap.createScaledBitmap(fullBitmap, scaleWidth, scaleHeight, true);
// 2. Instantiate the downsized image content as a byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
return baos.toByteArray();
}
所以,它是正确的吗?
try {
Bitmap fullBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
ByteArrayOutputStream out = new ByteArrayOutputStream();
fullBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); // Here 100 is the quality in percent of the image
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
int scaleWidth = fullBitmap.getWidth() / scaleDivider;
int scaleHeight = fullBitmap.getHeight() / scaleDivider;
byte[] downsizedImageBytes =
getDownsizedImageBytes(fullBitmap, scaleWidth, scaleHeight);
uploadTask = filerefrence.putBytes(downsizedImageBytes);
} catch (IOException e) {
e.printStackTrace();
}
您可以像这样使用 BitmapFactory
class
Bitmap original = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri)
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.JPEG, 100, out); // Here 100 is the quality in percent of the image
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
注意 PNG 已经是无损格式,所以压缩时不会有任何区别,如果你真的想看到效果使用 JPEG
质量接受 0 - 100
0 = 最大压缩(适用于小图像的最低质量)
100 = 最小压缩(适合大图像的最大质量)
感谢this回答
上传图片时如何压缩文件大小和质量?
我已经上传文件到firebase,如何压缩文件?
因为当文件很大时,recyclerview 加载会很慢。
所以我想压缩文件。
Uri imageUri = imageListUri.get(index);
if(spinner.getSelectedItem().toString().equals(getApplicationContext().getResources().getString(R.string.choose_sub))) {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle(getApplicationContext().getResources().getString(R.string.alert_error))
.setMessage(getApplicationContext().getResources().getString(R.string.choose_sub))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
alertDialog.show();
} else if(image_check.equals("ok")) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage(getApplicationContext().getResources().getString(R.string.posting));
progressDialog.show();
if (imageUri != null) {
final StorageReference filerefrence = storageReference.child(System.currentTimeMillis()
+ "." + getFileExtension(imageUri));
// scaling the image
int scaleDivider = 1;
try {
Bitmap fullBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
int scaleWidth = fullBitmap.getWidth() / scaleDivider;
int scaleHeight = fullBitmap.getHeight() / scaleDivider;
byte[] downsizedImageBytes =
getDownsizedImageBytes(fullBitmap, scaleWidth, scaleHeight);
uploadTask = filerefrence.putBytes(downsizedImageBytes);
} catch (IOException e) {
e.printStackTrace();
}
static public byte[] getDownsizedImageBytes(Bitmap fullBitmap, int scaleWidth, int scaleHeight) throws IOException {
Bitmap scaledBitmap = Bitmap.createScaledBitmap(fullBitmap, scaleWidth, scaleHeight, true);
// 2. Instantiate the downsized image content as a byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
return baos.toByteArray();
}
所以,它是正确的吗?
try {
Bitmap fullBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
ByteArrayOutputStream out = new ByteArrayOutputStream();
fullBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); // Here 100 is the quality in percent of the image
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
int scaleWidth = fullBitmap.getWidth() / scaleDivider;
int scaleHeight = fullBitmap.getHeight() / scaleDivider;
byte[] downsizedImageBytes =
getDownsizedImageBytes(fullBitmap, scaleWidth, scaleHeight);
uploadTask = filerefrence.putBytes(downsizedImageBytes);
} catch (IOException e) {
e.printStackTrace();
}
您可以像这样使用 BitmapFactory
class
Bitmap original = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri)
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.JPEG, 100, out); // Here 100 is the quality in percent of the image
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
注意 PNG 已经是无损格式,所以压缩时不会有任何区别,如果你真的想看到效果使用 JPEG
质量接受 0 - 100
0 = 最大压缩(适用于小图像的最低质量)
100 = 最小压缩(适合大图像的最大质量)
感谢this回答