gpt4 book ai didi

android - 图像未使用 MultipartEntity 上传

转载 作者:行者123 更新时间:2023-11-30 03:39:38 26 4
gpt4 key购买 nike

我想使用 MultipartEntity 上传单张图片。我尝试了以下代码。

但是图片没有上传。我没有收到任何错误。我已经为此添加了足够的库

  try {

HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
"http://192.168.1.6/uploadimg.php");
httpClient.getParams().setParameter(
CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
File f = null;
FileBody fo = null;

MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);

// code for send image using post method
f = new File("/mnt/sdcard/a.png");
fo = new FileBody(f);
reqEntity.addPart("uploaded", fo);

Log.i("uploaded", "image added Parameter added");

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("Upload photo", "Response" + s);

// return getUploadResponce(s.toString());
// Log.i("Response ", );

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

PHP文件

<?php

$file = $_FILES['uploaded']

move_uploaded_file($_FILES['uploaded']['tmp_name'], $_FILES['uploaded']['name']);

?>

最佳答案

好像获取图片路径有问题,改一行代码;

使用:

String xtStorageDirectory = Environment.getExternalStorageDirectory()+ "/a.png";
f = new File(xtStorageDirectory);

相反:

f = new File("/mnt/sdcard/a.png");

并确保您已授予访问 external storage 的权限在 list 文件中,下面一个。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于android - 图像未使用 MultipartEntity 上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16008574/

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