在安卓中通过本地JSON加载本地图片

7
我有一个本地的JSON文件,里面包含一些静态数据,这些数据将在列表视图中展示。每个对象都应该有一个相应的图片或Logo,在列表视图中也需要展示。这些图片将会存储在drawable文件夹中。
这个数据/列表视图类似于这个 http://wptrafficanalyzer.in/blog/android-itemclicklistener-for-a-listview-with-images-and-text/
在这个教程中,所有的数据和图片都被存储在数组中。我的数据则存储在JSON文件中。那么我该如何在JSON文件中“引用”图片或图片名称,并在创建列表视图时如何访问相关图片呢?

你需要使用JSON解析器或类似gson的工具将JSON数据转换为Java对象,然后再进行操作。 - Vince Blas
我已经完成了这个。我的问题是我应该如何存储和解析正确的图片? - user3658609
如果图像位于drawable文件夹中,则需要一种将数据对象与可绘制资源进行映射的方法,例如使用switch语句。如果图像是JSON对象的一部分,则可以考虑对图像进行Base64编码/解码。 - Vince Blas
为什么要做重复的工作。如果你的数据是本地的,那就使用数组。你正在创建JSON,然后又将值提取到数组中。从知识角度来说,这对于练习是有好处的。 - Vasudev Vyas
3个回答

8

假设您在drawable文件夹中有一些图像:

drawable
    image_name_1
    image_name_2
    image_name_3
    image_name_4
    ...

将图像名称放入JSON中:

[
    {
        "some_field_1": "some_value_1",
        "some_field_2": "some_value_2",
        "some_field_3": "some_value_3",
        ...
        "image_name": "image_name_1"
    },
    {
        "some_field_1": "some_value_1",
        "some_field_2": "some_value_2",
        "some_field_3": "some_value_3",
        ...
        "image_name": "image_name_2"
    },
    {
        "some_field_1": "some_value_1",
        "some_field_2": "some_value_2",
        "some_field_3": "some_value_3",
        ...
        "image_name": "image_name_3"
    },
    ...
]

从JSON中获取名称并加载可绘制资源:

    JSONArray data; // your JSON
    Context context; // context
    Resources resources = context.getResources();
    for (int i = 0; i < data.length(); i++) {
        // getting some another JSON field

        // get image name from JSON
        String imageName = data.getJSONObject(i).getString("image_name");

        // get resource id by image name
        final int resourceId = resources.getIdentifier(imageName, "drawable", context.getPackageName());

        // get drawable by resource id
        Drawable drawable = resources.getDrawable(resourceId);

        // get bitmap by resource id
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);
    }

更新

将图像资源id存入HashMap中

    ...
    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

    JSONArray data; // your JSON
    Context context; // context
    Resources resources = context.getResources();
    for (int i = 0; i < data.length(); i++) {
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("txt", "Country : " + countries[i]);
        hm.put("cur","Currency : " + currency[i]);

        // get image name from JSON
        String imageName = data.getJSONObject(i).getString("image_name");
        // get resource id by image name
        final int resourceId = resources.getIdentifier(imageName, "drawable", context.getPackageName());

        hm.put("flag", Integer.toString(resourceId) );
        aList.add(hm);
    }

我该如何将图像传递给哈希映射和列表视图?请查看此链接:http://wptrafficanalyzer.in/blog/android-itemclicklistener-for-a-listview-with-images-and-text/ 我基本上使用相同的设置来创建列表视图。谢谢! - user3658609

0

//如果您有一个模型类,那么这段代码可以帮助您

 try {

        JSONObject jsonObject = new JSONObject(loadJSONFromAsset());
        JSONArray array = jsonObject.getJSONArray("Details");

        Resources resources = this.getResources();

        for (int i=0;i<array.length();i++){
            // Parse the JSON
            JSONObject jo_inside = array.getJSONObject(i);
            String title = jo_inside.getString("title");
            String img = jo_inside.getString("img");

            // get resource id by image name
            final int resourceId = resources.getIdentifier(img, "drawable", this.getPackageName());

            //load Model Class
            Model_Home data = new Model_Home(title,resourceId);
            indexList.add(data);


        }

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

0
enter  try {

    JSONObject jsonObject = new JSONObject(loadJSONFromAsset());
    JSONArray array = jsonObject.getJSONArray("Details");

    Resources resources = this.getResources();

    for (int i=0;i<array.length();i++){
        // Parse the JSON
        JSONObject jo_inside = array.getJSONObject(i);
        String title = jo_inside.getString("title");
        String img = jo_inside.getString("img");

        // get resource id by image name
        final int resourceId = resources.getIdentifier(img, "drawable", this.getPackageName());

        //load Model Class
        Model_Home data = new Model_Home(title,resourceId);
        indexList.add(data);


    }

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

代码在这里


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接