douchongbang6011 2012-03-24 07:47
浏览 72

如何使用Android中的json从mysql数据库中检索图像

I am get the json data from php. and pass to android

Here is my php code:

<?php
   mysql_connect("localhost","root","MobixMySQL");
   mysql_select_db("cozydine");
   $q=mysql_query("SELECT itemimage from fooditem");
   while($obj=mysql_fetch_object($q)){
       $arr[]=$obj;
   }
   echo '{"names":'.json_encode($arr).'}';
   mysql_close();
?>

And here is how I read the image:

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 + "
");
    }
    is.close();
    result = sb.toString();
    jArray = new JSONObject(result);
    JSONArray json = jArray.getJSONArray("names");
    for (int i = 0; i < json.length(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        JSONObject e = json.getJSONObject(i);
        Qrimage=e.getString("itemimage");
        System.out.println(Qrimage);

        byte[] qrimage = Base64.decode(Qrimage.getBytes());

        System.out.println(qrimage);
        bmp = BitmapFactory.decodeByteArray(qrimage, 0,qrimage.length);
        ImageView imageview=(ImageView) findViewById(R.id.imageView1);
        imageview.setImageBitmap(bmp);

Here is how I parse the response from the php code:

codeInputStream is = null;
String result = "";
JSONObject jArray = null;
try {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(serverurl+"showall.php");
    HttpResponse response = httpClient.execute(httpPost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
} catch (Exception e) { }

In that way i can view only last image in my android mobile .. I want display all images from database into android mobile please help me how to get all images using json data

  • 写回答

1条回答 默认 最新

  • douxian1892 2012-03-24 09:59
    关注

    Low you've got quite a complex code there. However, I think your problem is very simple: you actually get all the images, but in those two lines:

    ImageView imageview=(ImageView) findViewById(R.id.imageView1);
    imageview.setImageBitmap(bmp);
    

    You overwrite the prevoius picture with the next one to come. Thus only the last one is displayed eventually. You can easily check that by changing the for cycle to:

    for (int i = 0; i < json.length() - 1; i++) {
    

    Then the second to last image should be shown. If this really is the case you will need to make out a way to display all the images simultanously.

    I just couldn't find any other problem in your code, but please if I got it wrong, write back so I can try to help more.

    EDIT As for how you can display the images together: you use image gallery. The image gallery is just another type of android widget (like the image view).

    In your activity do the following few lines (initialize your gallery):

    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 + "
    ");
        }
        is.close();
        result = sb.toString();
        jArray = new JSONObject(result);
        JSONArray json = jArray.getJSONArray("names");
        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this, json));
    

    Here I use image gallery adapter which I define as follows:

    public class ImageAdapter extends BaseAdapter {
    
        private ImageView[] mImages;
    
        public ImageAdapter(Context context, JSONArray imageArrayJson) {
            this.mImages = new ImageView[imageArrayJson.length];
            String qrimage;
    
            for (int i = 0; i < imageArrayJson.length(); i++) {
                JSONObject image = imageArrayJson.getJSONObject(i);
                qrimage = image.getString("itemimage");
    
                byte[] qrimageBytes = Base64.decode(qrimage.getBytes());
    
                bmp = BitmapFactory.decodeByteArray(qrimageBytes,
                                                    0,
                                                    qrimageBytes.length);
                mImages[i] = new ImageView(context);
                mImages[i].setImageBitmap(bmp);
                mImages[i].setLayoutParams(new Gallery.LayoutParams(150, 100));
                mImages[i].setScaleType(ImageView.ScaleType.FIT_XY);
            }
        }
    
        public int getCount() {
            return mImages.length;
        }
    
        public Object getItem(int position) {
            return position;
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            return mImages[position];
        }
    }
    

    And you need to declare this image gallery in the layout of your activity:

    <Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
    

    Hopefully this all will help you, however, I have written it in text editor, without trying, so it might be that I made some error. If so please write back.

    EDIT2 If you want to stack the images vertically just use ListView instead of the gallery. You will still need to set the adapter, but you can use the same one I already provided you with. However, keep in mind that the ListView will not necessarily show the images one by one.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序