将图片上传到服务器PHP Android

3

我现在正在尝试将图片上传到我的服务器,但却迷失了方向。我能够在 Android 设备上拍照并获取我的位置信息。下面是我用来上传文件到服务器的代码:

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

我将图片文件传递给这个函数,它会进行上传。但是我认为错误出现在服务器端。

以下是我的 PHP 代码:

      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>';
    }

文件已被访问,但仍保持空白。这里我是否遗漏了什么?请帮忙,我尝试在谷歌上搜索,但是得到的结果都没有帮到我太多。
2个回答

3
你可以在这里查看一个非常好的教程

还可以尝试以下代码,

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

0
如果您在基于Unix的平台上运行apache/php,请确保您的apache服务器对它正在尝试存储文件的目录具有写权限(根据您当前的代码,与PHP脚本所在的相同目录)。作为一个快速测试,在您的脚本所在的目录中执行chmod 0777 .,并查看上传的图像是否正确显示。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接