php - 从 PHP 服务器获取图像到 Android

标签 php android webserver android-image imagedownload

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

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

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

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

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

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

最佳答案

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

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); 

?> 

关于php - 从 PHP 服务器获取图像到 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6026825/

相关文章:

linux - SonarQube 返回错误的网关错误

php - 是多维数组好还是多维数组好?

PHP首先将数据发送到数据库然后验证

php - 如何停止 PHP 脚本?阻止发送电子邮件?

Android如何通过xml或程序绘制正多边形

android - proguardFile getDefaultDexGuardFile ('dexguard-release.pro' ) 不再有效

java - 如何在Android中使用Canvas Paint在圆弧顶部绘制线条路径

php - php登录表单验证问题

django - 分析 Django Web 服务器的高启动时间

PHP 似乎执行脚本两次