上传图片到服务器PHP的Andr​​oid [英] Upload Image to Server PHP Android

查看:146
本文介绍了上传图片到服务器PHP的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我迷路了,现在,试图图像上传到我的服务器。我能够拍摄照片,并获得在Android设备上我的位置。我有如下code上传文件到服务器:

I am lost right now with trying to upload an image to my server. I am able to take the picture and get my location on the Android device. I have the follow code to upload the file to the server:

    public Boolean postFunction(File image) {
    String tag = "postFunction";

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(UPLOAD_URL);

    try {
        MultipartEntity entity = new MultipartEntity();

        entity.addPart("type", new StringBody("photo"));
        entity.addPart("data", new FileBody(image));
        httppost.setEntity(entity);
        HttpResponse response = httpclient.execute(httppost);
        Log.i(tag, "picture was uploaded " + response.toString());
        return true;

    } catch (ClientProtocolException e) {
        Log.e(tag, "Client: " + e.toString());
        return false;
    } catch (IOException e) {
        Log.e(tag, "IO: " + e.toString());
        return false;
    }
}

我通过图像文件这一功能,它的上传。然而,错误在于在服​​务器端,我相信。

I pass the image file to this function and it does the upload. However the error lies on the server end I believe.

下面是我的PHP code:

Here is my PHP code:

      public function upload() {
        //$type = $this->input->post('type');
        //$data = $this->input->post('data');

        $base = $_REQUEST['data'];

        echo $base;

// base64 encoded utf-8 string

        $binary = base64_decode($base);

// binary, utf-8 bytes

        header('Content-Type: bitmap; charset=utf-8');

// print($binary);
//$theFile = base64_decode($image_data);

        $file = fopen('./test.jpg', 'wb');

        fwrite($file, $binary);

        fclose($file);

        echo '<img src=test.jpg>';
    }

该文件被感动了,但仍然存在空白。我失去了一些东西呢?请帮帮忙,我想谷歌搜索这一点,但提出了不同的结果并没有帮助我很多。

The file gets touched but still remain blank. Am I missing something here? Please help, I tried Googling this but came up with different results that did not help me much.

推荐答案

您可以选中一个非常好的教程的这里

You can check one very good tutorial here.

也可以尝试以下code,

Also try following code,

public class TryprojectActivity extends Activity {
    InputStream is;
    int pic_count = 0;
    Bitmap bitmap=null;
    FileInputStream in1,in2,in3;
    BufferedInputStream buf;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

        try {
            in1 = new FileInputStream("/sdcard/1.jpg");
        } 
        catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        try {
            in2 = new FileInputStream("/sdcard/2.jpg");
        } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 

    try {
        in3 = new FileInputStream("/sdcard/3.jpg");
    } 
    catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 

    Bitmap bitmapOrg1 = BitmapFactory.decodeStream(in1);
    ByteArrayOutputStream bao1 = new ByteArrayOutputStream();
    bitmapOrg1.compress(Bitmap.CompressFormat.JPEG, 90, bao1);
    byte [] imagearray1 = bao1.toByteArray();
    String ba1=Base64.encode(imagearray1);

    Bitmap bitmapOrg2 = BitmapFactory.decodeStream(in2);
    ByteArrayOutputStream bao2 = new ByteArrayOutputStream();
    bitmapOrg2.compress(Bitmap.CompressFormat.JPEG, 90, bao2);
    byte [] imagearray2 = bao2.toByteArray();
    String ba2=Base64.encode(imagearray2);

    Bitmap bitmapOrg3 = BitmapFactory.decodeStream(in3);
    ByteArrayOutputStream bao3 = new ByteArrayOutputStream();
    bitmapOrg3.compress(Bitmap.CompressFormat.JPEG, 90, bao3);
    byte [] imagearray3 = bao3.toByteArray();
    String ba3=Base64.encode(imagearray3);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);

    nameValuePairs.add(new BasicNameValuePair("image1",ba1));
    nameValuePairs.add(new BasicNameValuePair("image2",ba2));
    nameValuePairs.add(new BasicNameValuePair("image3",ba3));

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://helpdesk.cispl.com/upload_file.php");
        UrlEncodedFormEntity obj = new UrlEncodedFormEntity(nameValuePairs);
        obj.setChunked(true);
        httppost.setEntity(obj);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        //is = entity.getContent();
        httpclient.getConnectionManager().shutdown(); 
    }
    catch(Exception e){
        //CommonFunctions.writeLOG(ctx.getClass().toString(), e.toString());
        //CommonFunctions.showToast(ctx, "Unable to post captured image file: " +
        //e.toString());
    }
}

这篇关于上传图片到服务器PHP的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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