gpt4 book ai didi

android - 如何使用多部分形式的数据json将图像(位图)发送到android中的服务器

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:31:58 25 4
gpt4 key购买 nike

我有上传图片到服务器的代码,它可以工作,

HttpEntity resEntity;

HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(Constants.url_create_product);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File file= new File(path);
FileBody bin = new FileBody(file);

reqEntity.addPart("phone", new StringBody(mPhoneNumber));
reqEntity.addPart("prod_title", new StringBody(namapro));
reqEntity.addPart("prod_price", new StringBody(hargapro));
reqEntity.addPart("prod_desc", new StringBody(despro));
reqEntity.addPart("prod_order", new StringBody(orderpro));
reqEntity.addPart("prod_image", bin);

post.setEntity(reqEntity);
HttpResponse response = httpClient.execute(post);
resEntity = response.getEntity();
String response_str = EntityUtils.toString(resEntity);
Gson gson = new Gson();
gson.toJson(response_str);
if (resEntity != null) {
Log.i("RESPONSE",response_str);
runOnUiThread(new Runnable(){
public void run() {
try {
Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

但我有菜单图像编辑器。该编辑器是裁剪图像,那是像这样的代码返回位图值

Bundle extras = data.getExtras();

if (extras != null) {
photo = extras.getParcelable("data");

mImageView.setImageBitmap(photo);
}

File f = new File(mImageCaptureUri.getPath());

if (f.exists()) f.delete();

break;

我想问一下,如何解决将带有参数位图的图像发送到服务器。如您所知,我发送图像的代码现在使用参数路径(字符串)。

最佳答案

ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);

然后将此编码图像作为 String 发送,在您的服务器中您需要对其进行解码以获取图像本身。

关于android - 如何使用多部分形式的数据json将图像(位图)发送到android中的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12796579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com