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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序