2017-09-08 48 views
0

如何将我的数据库中的图像加载到android应用程序中并将其放入列表视图中。数据库是MySQL和图像以PNG格式存储如何在android中加载图像

这是我的代码检索我的数据库中的数据。该a_emblem是ImageView的,在我的JSON镜像文件

private void showResult() { 
     JSONObject jsonObject; 
     ArrayList<HashMap<String, String>> list = new ArrayList<>(); 
     try { 
      jsonObject = new JSONObject(JSON_STRING); 
      JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY1); 

      for (int i = 0; i < result.length(); i++) { 
       JSONObject jo = result.getJSONObject(i); 
       String a_shortcut = jo.getString(Config.TAG_a_shortcut); 
       String a_emblem = jo.getString(Config.TAG_a_emblem); 
       String gold = jo.getString(Config.TAG_gold); 
       String silver = jo.getString(Config.TAG_silver); 
       String bronze = jo.getString(Config.TAG_bronze); 
       String total = jo.getString(Config.TAG_total); 
       HashMap<String, String> match = new HashMap<>(); 
       match.put(Config.TAG_a_shortcut, a_shortcut); 
       match.put(Config.TAG_a_emblem, a_emblem); 
       match.put(Config.TAG_gold, gold); 
       match.put(Config.TAG_silver, silver); 
       match.put(Config.TAG_bronze, bronze); 
       match.put(Config.TAG_total, total); 
       list.add(match); 

      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     ListAdapter adapter = new SimpleAdapter(
       getActivity(), list, R.layout.standlayout, 
       new String[]{Config.TAG_a_shortcut, Config.TAG_a_emblem, Config.TAG_gold, Config.TAG_silver, Config.TAG_bronze, Config.TAG_total}, 
       new int[]{R.id.shortcut, R.id.img, R.id.gold, R.id.silver, R.id.bronze, R.id.total}); 

     lv.setAdapter(adapter); 
    } 

How the Image are stored in my database

+0

我不认为这可以回答没有更多的上下文。这些图像如何存储在数据库中?什么是数据库呢? –

+0

你应该使用一个自定义的Adapter类来做到这一点。 – sam

+0

@ KenY-N添加了数据库和图片如何存储 – orange

回答

0

这里是你可以做什么。

  1. 将图像转换为位图。

  2. 将图像转换为base64字符串并将此base64字符串保存到数据库中。

  3. 在适配器中使用base64字符串时将其转换回图像。

  4. 在ImageView中设置位图。

它应该肯定工作。代码以供参考

  1. 转换可绘制为位图

    Bitmap icon = BitmapFactory.decodeResource(context.getResources(), 
                R.drawable.icon_resource); 
    
  2. 使用以下方法将位图转换为一个字节数组:

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); 
    byte[] byteArray = byteArrayOutputStream .toByteArray(); 
    

    到一个base64串从字节编码array:

    String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT); 
    

    在数据库中保存encoded

  3. 转换中的Base64字符串返回到Bitmap

    byte[] decodedString = Base64.decode(encoded , Base64.DEFAULT); 
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
    
  4. 将位图图像视图:

    imageView.setImageBitmap(bitmap); 
    
0

使用毕加索或滑行这里是网址为您

Picasso

Glide