Android - 使用php将图像上传到服务器的最佳方式


Android - best way to upload image to the server using php

我需要将位图图像上传到我的服务器,但在上传时需要花费大量时间。我使用base64对图像进行编码和解码,同时将其作为Blob添加到数据库中。

我想知道上传图像的另一种替代方法是什么,哪种方法更有效。

这是我一直在尝试的:

HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
HttpConnectionParams.setConnectionTimeout(params, 20000);
HttpConnectionParams.setSoTimeout(params, 15000);
DefaultHttpClient httpClient = new DefaultHttpClient(params);
HttpPost httpPost = new HttpPost(url);
namevaluepair.add(new BasicNameValuePair("icon", getStringDrawing(bmIcon)))
httpPost.setEntity(new UrlEncodedFormEntity(namevaluepair));
httpClient.execute(httpPost);
public static String getStringDrawing(Bitmap bm) {
    String image_str = "";
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        byte[] byte_arr;
        bm.compress(Bitmap.CompressFormat.PNG, 90, stream);
        byte_arr = stream.toByteArray();
        image_str = Base64.encodeBytes(byte_arr);
    } catch (NullPointerException e) {
    }
    return image_str;
}

在PHP中,我试图将其保存在Blob和文件夹中的一些代码中:

$Drawing = $_POST {'drawing'};
$buffer = base64_decode($Drawing);
$file = fopen("PostImage/P".$ID.".png", 'wb');
fwrite($file, $buffer);
fclose($file);

第二次尝试:

$Drawing = $_POST {'drawing'};
$buffer = base64_decode($Drawing);
$buffer = mysql_real_escape_string($buffer);
$query = "INSERT INTO `drawposts` (`PostID`, `UserID`,`DatePost`, `Drawing`) VALUES (NULL,'$UserID','$Date','$buffer')";
$sth = mysql_query($query);

查看 FileEntity 。类似的东西

File Picture = new File("/path/to/jpg");
FileEntity f = new FileEntity(picture,"application/octet-stream");
HttpPost post = new HttpPost("my URL");
post.setEntity(f);
post.setHeader("Content-type", "application/octet-stream");