上传到服务器时如何压缩图像?
我一直在试图看看压缩图像的其他例子。但是,我仍然不知道在哪里以及如何将代码压缩到。有人能帮助我吗?上传到服务器时如何压缩图像?
public void uploadMultipart() {
//getting name for the image
String name = editText.getText().toString().trim();
//getting the actual path of the image
String path = getPath(filePath);
//Uploading code
try {
String uploadId = UUID.randomUUID().toString();
//Creating a multi part request
new MultipartUploadRequest(this, uploadId, Constants.UPLOAD_URL)
.addFileToUpload(path, "image") //Adding file
.addParameter("name", name) //Adding text parameter to the request
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload(); //Starting the upload
} catch (Exception exc) {
Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
}
}
//method to get the file path from uri
public String getPath(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
cursor.close();
cursor = getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
// Bitmap bmp = BitmapFactory.decodeFile(path);
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
cursor.close();
return path;
}
下面是压缩图像中的位图
下面代码JPEG图像
Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open("imagename.png"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); // you can set as 90 for compress ration
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
下面PNG图像代码的代码:
Bitmap bitmap= BitmapFactory.decodeStream(getAssets().open("imagename.png"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
否则,这里是代码它编码字符串并以编码格式发送到服务器图像
String encodedString ="";
try {
BitmapFactory.Options options = null;
options = new BitmapFactory.Options();
options.inSampleSize = 1;
bitmap = BitmapFactory.decodeFile(filepath, options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Must compress the Image to reduce image size to make upload easy
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] byte_arr = stream.toByteArray();
// Encode Image to String
encodedString = Base64.encodeToString(byte_arr, 0);
} catch (Exception e) {
e.printStackTrace();
}
尝试以上解决方案。它会为我工作。
谢谢!我之前看到过这个,但我不确定我在哪里放置这段代码? – arsenallavigne
从厨房或内部存储器中检索图像路径后。该路径将采用字符串格式。该图像使用上述代码进行压缩并发送到压缩图像。 –
我试过使用上面的方法,但仍然无法解决它。我想我可能错误地执行了。 – arsenallavigne
它是什么类型的文件?如果它的JPG或PNG,它已经压缩。唯一不会是.bmp的文件不太可能。 –
@GabeSechan这是JPG或PNG,但我看到很多例子,虽然当我搜索如何压缩图像 – arsenallavigne
这两个都已经压缩。如果您重新压缩JPG,由于jpeg是有损压缩,将会失去质量。如果你重新压缩PNG,它会给你完全相同的文件(除非你把它压缩成jpg,在这种情况下你会失去质量)。如果你对质量损失还可以,你可能会压缩更多的png,但jpegs已经完成了。一般你不想失去质量。 –