私は画像の圧縮のための他の例を見ようとしています。しかし、私はまだどこにどのように私は圧縮のためのコードを含めるか分からない。誰も私にこれを手伝ってもらえますか?サーバーにアップロードするときにイメージを圧縮するにはどうすればよいですか?
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を圧縮することができるかもしれませんが、jpegはすでに完了しています。そして、一般的に品質を失うことは望ましくありません。 –