【问题标题】:Android - best way to upload image to the server using phpAndroid - 使用 php 将图像上传到服务器的最佳方式
【发布时间】:2013-10-11 12:32:50
【问题描述】:

我需要将位图图像上传到我的服务器,但上传需要很长时间。我使用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);

【问题讨论】:

    标签: php android mysql image upload


    【解决方案1】:

    查看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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2012-08-13
      • 2016-07-20
      • 2011-02-02
      • 2015-03-29
      • 2011-10-15
      相关资源
      最近更新 更多