我一直在試圖看看壓縮圖像的其他例子。但是,我仍然不知道在哪裏以及如何將代碼壓縮到。有人能幫助我嗎?上傳到服務器時如何壓縮圖像?
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;
}
它是什麼類型的文件?如果它的JPG或PNG,它已經壓縮。唯一不會是.bmp的文件不太可能。 –
@GabeSechan這是JPG或PNG,但我看到很多例子,雖然當我搜索如何壓縮圖像 – arsenallavigne
這兩個都已經壓縮。如果您重新壓縮JPG,由於jpeg是有損壓縮,將會失去質量。如果你重新壓縮PNG,它會給你完全相同的文件(除非你把它壓縮成jpg,在這種情況下你會失去質量)。如果你對質量損失還可以,你可能會壓縮更多的png,但jpegs已經完成了。一般你不想失去質量。 –