【问题标题】:Android: how to upload image in mysql using phpAndroid:如何使用 php 在 mysql 中上传图片
【发布时间】:2013-01-23 15:36:21
【问题描述】:

我在 ImageView 中有用户选择的图像...如何使用 php 将其插入 mysql 数据库表?

谁能帮帮我。

我希望图像保存在上面的列类型是 Blob。

【问题讨论】:

  • @jtheman imageView.buildDrawingCache(); bmIcone = imageView.getDrawingCache(); ByteArrayOutputStream 流 = 新的 ByteArrayOutputStream(); bmIcone.compress(Bitmap.CompressFormat.PNG, 90, 流); byte [] byte_arr = stream.toByteArray();字符串 image_str = Base64.encodeBytes(byte_arr); namevaluepair.add(new BasicNameValuePair("image",image_str)); php 代码 $base= $_REQUEST['image']; $buffer = base64_decode($base);

标签: php android mysql image


【解决方案1】:

首先,您必须在 android 中调用您的 php。这个话题已经有很多答案了:

为了回答您的问题,本教程教您如何编写 php 以在 MySQL 中存储图像:

【讨论】:

  • 感谢您的帮助,但您给我的教程是针对 html 页面的。我认为android是不同的..我现在正在做的是将ImageView转换为位图,然后通过使用base64对其进行编码将位图转换为字符串,然后将其发送到php并对其进行解码...问题是我怎么能解码后将其插入表中? ...或者是否有任何不同的解决方案可以将图像插入 mysql 表?
【解决方案2】:

好的,我为我的问题找到了解决方案

假设图像已经转换为位图的java代码:

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmIcone.compress(Bitmap.CompressFormat.PNG, 90, stream);
        byte[] byte_arr = stream.toByteArray();
        String image_str = Base64.encodeBytes(byte_arr);
        namevaluepair.add(new BasicNameValuePair("image", image_str));

在服务器中:

$base= $_REQUEST['image'];
    $buffer = base64_decode($base);

    $buffer = mysql_real_escape_string($buffer);

然后将 $buffer 插入表 blob 列类型

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-04
    • 2021-03-30
    • 2017-06-05
    • 2015-01-17
    • 2016-07-20
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多