使用多httppost方法不张贴图像文件中的android [英] Not Posting image file in android using multipart httppost method

查看:207
本文介绍了使用多httppost方法不张贴图像文件中的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是由<借来的href="http://stackoverflow.com/questions/16483072/how-to-upload-image-in-to-php-server-using-multipart">same问题,怎么一回事,因为我在服务器端面临着同样的problem.During形象发布,图像的细节无法得到服务器端。像这样的:

This question is borrowed from the same question, beacuse i faced same problem.During image posting in server side, image details could not getting in server side. Like this:

无法发布图像文件信息PHP服务器,用户标识确定,但图像文件的信息无法post.In这种情况下,图像保存成功在服务器上,我坚持不下去了图像的PHP SEVER信息。

Failed to post image file information php server, userid ok but image file information could not post.In this case, Image save successfully on server, i could not get information on php sever of image.

下面是code:

公共类UploadImageHttpPost {

public class 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);

    }
}

我得到这样的:

I get Like this:

Array
(
    [UserId] => 1
)

在那里我在PHP测试用这个,什么从多部分卡梅斯:

where i using this in php for testing , what cames from multipart:

$ Filedata上= $ _FILES ['形象'];
  $ F = FOPEN(时间(),世界银行,TXT);
  FWRITE($ F,的print_r($ _ REQUEST,真));
  fclose函数($ F);

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

但在这里以下部分缺失意味着我没有得到,没有任何遗漏code多部分POST方法请查看上面的Java code:

But here below part missing means i am not getting, is there any missing code in multipart post method please check above java code:

[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]

I following this tutorials also: [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 和许多其他的从 Google搜索,但没有找到妥善的解决办法。

[2]: http://www.webspeaks.in/2012/08/upload-files-from-android-to-server.html and lots of other from the googled, but could not find proper solution.

推荐答案

我最近在做,就像你的问题 下面是解决方法:

I recently doing just like your problem Here is the Solution:

取图像从SD卡和相机:

Image take from sdcard and camera:

@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);

            }
        }

现在你调出功能,在这里你要上传的图片:

Now you call the Function, where you want to upload pictures:

    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);

    }
}

我希望你满意这个codeI成功运行。

I hope you satisfied for this code.I run successfully.

这篇关于使用多httppost方法不张贴图像文件中的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆