【问题标题】:Get images from PHP server to Android从 PHP 服务器获取图像到 Android
【发布时间】:2011-05-17 05:59:04
【问题描述】:

我正在制作一个 android 应用程序的原型,需要将图像从 web 服务器获取到 Android ListView。我通过这个线程Lazy load of images in ListView了解了事物的android方面(尽管尚未实现和测试)

我需要有关它的 PHP/服务器端的帮助。需要在列表视图中填充的特定图像将动态决定(具有给定标签的图像)。我是 Web 应用程序的新手,但我设法设置了一个可以运行 PHP 脚本的 Web 服务器。

1) Android 决定检索哪些标签。建立 HTTP 连接并使用给定标记作为参数调用 PHP 脚本。

2) PHP 脚本在 MySQL 数据库中查找标签,确定要上传的图像的 ID,这些图像存储在一个文件夹中。

3) [这里我不清楚如何实现] PHP 发送这些图像,然后是与每个图像关联的元数据。发送的记录数不会预先固定,由 MySQL 返回的记录数决定(只有最大值是固定的)。

我将不胜感激任何示例脚本,以及指向哪里可以找到更多信息以更好地了解内部工作的指针。此外,如果有更好的框架来做我想做的事,我很高兴了解这一点(我没有嫁给 PHP/MySQL)。

【问题讨论】:

    标签: php android webserver android-image imagedownload


    【解决方案1】:

    这是获取单个图像并显示在图像视图中的代码。

    public class Database_demo extends Activity {
    
    public static final int DIALOG_DOWNLOAD_JSON_PROGRESS = 0;
    
    int[] flags;
    String strImageName;
    int n = 10000;
    String[] mySecondStringArray = new String[n];
    String[] strArray;
    
    HashMap<String, String> hm;
    List<HashMap<String, String>> aList;
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;
    private String Qrimage;
    private Bitmap bmp;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news);
    
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(
                    "http://192.168.1.50/XXXX/getimage.php");
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        } catch (Exception e) {
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            jArray = new JSONObject(result);
            JSONArray json = jArray.getJSONArray("tablename");
            for (int i = 0; i < json.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject e = json.getJSONObject(i);
                Qrimage = e.getString("imagefieldname");
                System.out.println(Qrimage);
    
                byte[] qrimage = Base64.decode(Qrimage.getBytes(), i);
    
                System.out.println(qrimage);
                bmp = BitmapFactory.decodeByteArray(qrimage, 0, qrimage.length);
                ImageView imageview = (ImageView) findViewById(R.id.flag);
    
                imageview.setImageBitmap(bmp);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
      }
    

    PHP 文件

    <?php
    $host="localhost"; //replace with database hostname 
    $username="root"; //replace with database username 
    $password=""; //replace with database password 
    $db_name="databasename"; //replace with database name
    
    $con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    
    
    
    $sql = "SELECT * FROM tablename"; 
    
    $result1 = mysql_query($sql);
    
    $json = array();
    
    if(mysql_num_rows($result1)){
    while($row=mysql_fetch_assoc($result1)){
    $json['tablename'][]=$row;
    }
    }
    
    
    mysql_close($con);
    echo json_encode($json); 
    
    ?> 
    

    【讨论】:

      【解决方案2】:

      我猜您应该使用 JSON 通过 HTTP 连接将数据发送到您的移动设备。对于低互联网连接,我不喜欢 SOAP。

      如果你不能直接通过 HTTP URL 加载图片,你可以发送 JSON 格式的图片数据,以及 mime 类型,并通过图片数据创建图片对象。

      【讨论】:

      • 谢谢,瓦卡尔。对于 JSON 和 SOAP,你有什么特别的论据吗?为什么我更喜欢 JSON?我也很感激任何示例脚本。再次感谢。
      • 是的,我正在做一个使用soap的项目,当时这不是一个好主意,因为SOAP是一个通过http运行的协议,所以性能很慢。它通常用于开发 api 供第三方使用,如果您在双方都处理自己的代码,则必须使用 JSON 或 XML。或者如果您认为 api 调用不应该公开而不是实现 oauth。看看 facebook、twitter 等 sdks!
      【解决方案3】:

      如果我是你,我会做一个 php webservice nosoap http://android-developers.blogspot.com/2009/05/painless-threading.html

      用于下载图片。 Api 我会使用 ksoap2 dowload

      其余的阅读很多,但实际上很容易:D

      【讨论】:

      • nusoap 和 php 阅读 link
      • 对于低互联网连接我不喜欢肥皂。改用 json
      【解决方案4】:

      我使用了 InputStream 和 OutputStream,就像您使用 Java 下载文件一样。首先,我从 PHP/JSON 脚本中获取文件名(在服务器上)。当然,可以优化此方法,以清除缓存文件夹中的旧文件。

      private static File getImage(String filename) {
          String localFilename = new File(filename).getName();
      
          File img = new File("/sdcard/app/tmp/" + localFilename);
      
          // Create directories
          new File("/sdcard/app/tmp/").mkdirs();
      
          // only download new images
          if (!img.exists()) {
              try {
                  URL imageUrl = new URL("http://example.com/images/" + filename);
                  InputStream in = imageUrl.openStream();
                  OutputStream out = new BufferedOutputStream(new FileOutputStream(img));
      
                  for (int b; (b = in.read()) != -1;) {
                      out.write(b);
                  }
                  out.close();
                  in.close();
              } catch (MalformedURLException e) {
                  img = null;
              } catch (IOException e) {
                  img = null;
              }
          }
          return img;
      }
      

      【讨论】:

      • 有了这个解决方案,我似乎需要为每个文件下载单独连接。考虑到我每次可能要下载 10 张图片,是否有开销?另外,我需要获取与每个图像相关联的元数据。
      • 确实如此,但我不明白如何一次下载多个图像。我首先使用 JSON 下载元数据(包括文件名),然后添加一个 for 循环,使用此方法下载每个图像。
      • 谢谢,我会试试这个方法。实际上,即使文件名已知,文件夹中的图像文件夹也不会暴露给客户端。不确定这些情况的最佳做法是什么。
      猜你喜欢
      • 2011-10-27
      • 2015-06-03
      • 2018-07-04
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 2011-02-02
      • 2015-03-29
      • 2011-10-15
      相关资源
      最近更新 更多