doulu6929 2012-11-21 05:27
浏览 80
已采纳

如何从MySQL获取Android图像

I have been working hard in this app and have come into a problem of late. I am trying to store images (or paths to those images) in a database on my local network and retrieve them from my android application. I have been able to use PHP and JSON to retrieve text information but am not sure where to start for images. Should I store the actual images in the MySQL database or paths to images? How would I be able to keep them in a neat format? Any information you guys can offer is greatly appreciated.

  • 写回答

2条回答 默认 最新

  • dongque20030402 2012-11-21 05:39
    关注

    Just store the path in a database. Create a simple table that might have the following fields:

    path - filepath or filename to the image on the server id - numeric auto_increment id that is useful while retrieving or identifying images width - optional, but might come in handy because storing height/width is quicker than calculating it upon retrieval height - same deal as width category - useful if you want to display more than one image a time, this could help you group them and decide which ones belong together. give the same category value for images that belong together

    In order to retrieve the information for a certain picture, you could call it in one of two ways. The most likely solution would be to retrieve images based on their filename.

    $con = mysql_connect('host', 'user', 'pass');
    $db = mysql_select_db('mydb');
    
    $imgName = 'picture.jpg';
    $getImg = mysql_query("SELECT * FROM mypics WHERE path='" . $imgName . "' ORDER BY id DESC LIMIT 1");
    $imgSrc = mysql_result("path", 0, $getImg);
    echo '<img src="' . $imgName . '" />';
    

    Of course this seems a bit useless, because we already know the filename to begin with because we are storing it in $imgName and using it to search the database for the entry, but you could search based on ID, category, width, or height all the same way. However, this will only retrieve the first image returned. Likewise, the images are being sorted by most recent because of the "ORDER BY id DESC" call. If you want to be able to retrieve all the images returned by the query simply remove the "LIMIT 1" from the query and change the "0" in mysql_result() to whichever entry you want to retrieve. Note that results are 0-indexed like arrays, so if you want the 5th result, you would get result 4.

    If you want to display all images in a category, you can do something like this:

    $con = mysql_connect('host', 'user', 'pass');
    $db = mysql_select_db('mydb');
    
    $getImgs = mysql_query("SELECT * FROM mypics WHERE category='android' ORDER BY id DESC");
    while( ($i = mysql_fetch_array($getImgs) ) {
        echo '<img src="' . $i['path'] . '" />';
    }
    

    This would display all images from the category "android", starting with the most recent. Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C