gpt4 book ai didi

android - 不使用多部分httppost方法在android中发布图像文件

转载 作者:行者123 更新时间:2023-11-29 16:07:51 24 4
gpt4 key购买 nike

本题借自same question ,因为我遇到了同样的问题。在服务器端发布图片时,图片细节无法进入服务器端。像这样:

上传图片文件信息php服务器失败,userid ok但是图片文件信息无法上传。在这种情况下,图片成功保存在服务器上,我无法获取图片的php服务器信息。

代码如下:

公共(public)类 UploadImageHttpPost {

String testpath="/mnt/sdcard/14111.jpg";
String url = "http://andtuts.com/uploadImage.php";

public static void sendPost(String url, String imagePath,String userId)
throws IOException, ClientProtocolException {
String responseBody;

MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);

File file = new File(imagePath);
FileBody encFile = new FileBody(file,"image/jpeg");
entity.addPart("images", encFile);
entity.addPart("UserId", new StringBody(userId));
HttpPost request = new HttpPost(url);
request.setEntity(entity);

HttpClient client = new DefaultHttpClient();

ResponseHandler<String> responsehandler = new BasicResponseHandler();
responseBody = client.execute(request, responsehandler);

if (responseBody != null && responseBody.length() > 0) {
Log.w("TAG", "Response from image upload->" + responseBody);

}
}

我是这样的:

Array
(
[UserId] => 1
)

我在 php 中使用它进行测试的地方,来自 multipart 的内容:

$filedata = $_FILES['images'];
$f = fopen(time().".txt",'wb');
fwrite($f, print_r($_REQUEST,true));
fclose($f);

但是下面的部分缺失意味着我没有得到,multipart post 方法中是否有任何缺失的代码请检查上面的 java 代码:

[images] => Array
(
[name] => ball.png
[type] => image/png
[tmp_name] => /tmp/phpiQHIXQ
[error] => 0
[size] => 1664972
)

我也在学习这个教程:[ http://www.webspeaks.in/2012/08/upload-files-from-android-to-server.html][2]

[2]: http://www.webspeaks.in/2012/08/upload-files-from-android-to-server.html和许多其他来自谷歌搜索,但找不到合适的解决方案。

最佳答案

我最近在做你的问题这是解决方案:

从 SD 卡和相机拍摄的图像:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap scaledphoto = null;
int height = 100;
int width = 100;
if (resultCode == RESULT_OK) {
if (requestCode == SD_REQUEST) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
yourSelectedImage = BitmapFactory.decodeFile(selectedImagePath);
scaledphoto = Bitmap.createScaledBitmap(yourSelectedImage,
height, width, true);
pImage.setImageBitmap(scaledphoto);

Uri selectedImageUri1 = data.getData();
path = getRealPathFromURI(selectedImageUri1);


}
if (requestCode == CAMERA_REQUEST) {
yourSelectedImage = (Bitmap) data.getExtras().get("data");
scaledphoto = Bitmap.createScaledBitmap(yourSelectedImage,
height, width, true);
profImage.setImageBitmap(scaledphoto);
Uri selectedImageUri1 = data.getData();
path = getRealPathFromURI(selectedImageUri1);

}
}

现在调用Function,要上传图片的地方:

    String url="http://test.com/uploadimage.php";
//path is the defined, which is take from camera and sdcard.
// userid is, which user do you want upload picture.
UploadImageHttp.sendPost(url, path, userId);

这是上传图片的类:

public class UploadImageHttp {
public static void sendPost(String url, String imagePath,String userId)
throws IOException, ClientProtocolException {

String responseBody;
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);

MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);

File file = new File(imagePath);
ContentBody encFile = new FileBody(file,"image/png");

entity.addPart("images", encFile);
entity.addPart("UserId", new StringBody(userId));

request.setEntity(entity);



ResponseHandler<String> responsehandler = new BasicResponseHandler();
responseBody = client.execute(request, responsehandler);


if (responseBody != null && responseBody.length() > 0) {
Log.w("TAG", "Response image upload" + responseBody);

}
}

我希望你对这段代码满意。我运行成功。

关于android - 不使用多部分httppost方法在android中发布图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16506594/

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