使用下面的代碼上傳影像服務器 -Android的圖片上傳AWS服務器
final InputStream fileInputStream = MyApplication.getInstance().getContentResolver().openInputStream(imageFile);
bitmap = BitmapFactory.decodeStream(fileInputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
final byte[] bitmapData = byteArrayOutputStream.toByteArray();
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
// Add binary body
if (bitmap != null) {
ContentType contentType = ContentType.create("image/png");
builder.addBinaryBody("", bitmapData, contentType, "");
final HttpEntity httpEntity = builder.build();
StringRequest request =
new StringRequest(Request.Method.PUT, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public byte[] getBody() throws AuthFailureError {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
httpEntity.writeTo(bos);
} catch (IOException e) {
VolleyLog.e("IOException writing to ByteArrayOutputStream");
}
return bos.toByteArray();
}
};
這是上傳圖像細膩,但文件
--0Iw7PkPg_BhQghFGBR1_lBhO1RaCaBsZJ-U
Content-Disposition: form-data; name=""; filename=""
Content-Type: image/png
âPNG
....
--0Iw7PkPg_BhQghFGBR1_lBhO1RaCaBsZJ-U--
中添加人體頭部如果我刪除從該特定內容該文件,圖像可以很容易地用作PNG。有沒有辦法只上傳PNG文件部分到服務器?
嘗試使用亞馬遜sdk轉移實用程序 – g7pro