現在のアプリでは、サーバーに写真を送信しています。今、これらの写真が時々大きすぎるという問題があります。サーバーに送信する前に画像を縮小する最善の方法は何ですか。現在、画像の URI をデータベース (/mnt/sdcard/DCIM/SF.png) に保存し、サーバーに送信しています。必要なディスク容量が少なくなるように、画像の解像度を縮小したいと考えています。Androidで画像を変換する方法はありますか?
誰かがこれを良い方法で解決する方法を教えてもらえますか?
ありがとう
現在のアプリでは、サーバーに写真を送信しています。今、これらの写真が時々大きすぎるという問題があります。サーバーに送信する前に画像を縮小する最善の方法は何ですか。現在、画像の URI をデータベース (/mnt/sdcard/DCIM/SF.png) に保存し、サーバーに送信しています。必要なディスク容量が少なくなるように、画像の解像度を縮小したいと考えています。Androidで画像を変換する方法はありますか?
誰かがこれを良い方法で解決する方法を教えてもらえますか?
ありがとう
次のコードを使用して画像をサーバーに送信しました。ここでは、画像をバイト配列に変換します。ここでは、最初に URI から画像をデコードしてから、ビットマップをバイト配列に変換する必要があります。
URI からビットマップをデコードするには:
Bitmap imageToSend= decodeBitmap("Your URI");
URIをデコードする方法
Bitmap getPreview(URI uri) {
File image = new File(uri);
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image.getPath(), bounds);
if ((bounds.outWidth == -1) || (bounds.outHeight == -1))
return null;
int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
: bounds.outWidth;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = originalSize / THUMBNAIL_SIZE;
return BitmapFactory.decodeFile(image.getPath(), opts);
}
画像をサーバーに送信するコード
try {
ByteArrayOutputStream bosRight = new ByteArrayOutputStream();
imageToSend.compress(CompressFormat.JPEG, 100, bosRight);
byte[] dataRight = bosRight.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("url to send");
ByteArrayBody babRight = new ByteArrayBody(dataRight,
"ImageName.jpg");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("params", babRight);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
Log.v("Response value is", sResponse);
JSONObject postObj = new JSONObject(sResponse);
String successTag = postObj.getString("success");
if (successTag.equals("1")) {
imageDeletion();
} else {
}
}
} catch (Exception e) {
Log.e(e.getClass().getName(), e.getMessage());
}