カメラから最大解像度 (12mpx など) の大きな画像を投稿する必要があります。しかし、ファイルストリームをデコードして byteArrayInputStream を取得すると、OutOfMemoryError が発生することがよくあります。大きな画像を投稿する他の方法はありますか?
Ps この写真を表示したり拡大縮小したりする必要はありません。
はい、MultipartEntity で画像/ファイルを投稿できます。以下のサンプル スニペットを見つけてください。
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
File file= new File(filePath);
if(file.exists())
{
entity.addPart("data", new FileBody(file));
}
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
マルチパート エンティティを使用するには、httpmime-4.1.2.jar をダウンロードして、プロジェクトのビルド パスに追加する必要があります。
以上のレベルandroid:largeHeap="true"
を使用している場合は、アプリケーションレベルでマニフェストでこの行を使用してみてください API
11
元の画像形式で投稿できる場合は、ファイル ストリームから直接データを送信します。
FileInputStream imageIputStream = new FileInputStream(image_file);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
OutputStream out = connection.getOutputStream();
copyStream(imageIputStream, out);
out.close();
imageIputStream.close();
copyStream 関数:
static int copyStream(InputStream src, OutputStream dst) throws IOException
{
int read = 0;
int read_total = 0;
byte[] buf = new byte[1024 * 2];
while ((read = src.read(buf)) != -1)
{
read_total += read;
dst.write(buf, 0, read);
}
return (read_total);
}