StackOverflowやその他の役立つWebサイトのリソースを使用して、カメラアプリケーションで撮影した画像をAndroid携帯にアップロードできるアプリケーションを作成することに成功しました。唯一の問題は、私が今持っている電話は非常に高品質の写真を撮るため、アップロードの待ち時間が長くなることです。
画像をjpegから低レート(小さいサイズまたはWebに適したサイズ)に変換する方法について読みましたが、現在使用しているコードは、キャプチャされた画像をバイトとして保存します(以下のコードを参照)。画像の品質を元の形式に戻す方法はありますか、それとも画像をjpegに変換して画質を下げてから、バイト形式に戻す方法を見つける必要がありますか?
これが私が扱っているコードスニペットです:
if (Intent.ACTION_SEND.equals(action)) {
if (extras.containsKey(Intent.EXTRA_STREAM)) {
try {
// Get resource path from intent callee
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
// Query gallery for camera picture via
// Android ContentResolver interface
ContentResolver cr = getContentResolver();
InputStream is = cr.openInputStream(uri);
// Get binary bytes for encode
byte[] data = getBytesFromFile(is);
// base 64 encode for text transmission (HTTP)
int flags = 1;
byte[] encoded_data = Base64.encode(data, flags);
// byte[] encoded_data = Base64.encodeBase64(data);
String image_str = new String(encoded_data); // convert to
// string
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",
image_str));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://xxxxx.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String the_string_response = convertResponseToString(response);
Toast.makeText(UploadImage.this,
"Response " + the_string_response,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(),
Toast.LENGTH_LONG).show();
System.out.println("Error in http connection "
+ e.toString());
}
}
}
}