B
B
burguy832019-12-13 12:47:50
Java
burguy83, 2019-12-13 12:47:50

How to display image from json to apk?

Hello, I ask to help a beginner android programmer.
I am making an application with tests. My application displays a question and answers. I want to add the functionality of adding another photo to the questions. The questions are taken from the json file. My thoughts are to add another key-value pair to the json file already with pictures. I guess pictures are better kept in the drawable folder. And the questions begin
1. What is the path to this photo in the json file?
{
"question": "What does the coastal information sign shown in the illustration mean?",
"answers": [
"Maximum speed for displacement vessels 15 km/h",
"Breadth of the channel 15 m",
"Depth of the river 15 m",
"Air crossing height 15 m"
],
"correct_answer": 3,
"question_category": "1",
"picture": "/res/drawable/v1b1.jpg" <------- is the name of the photo. "this path will be a relative path to it?
}
2. How to display pictures from json to questions, and even so that they scale. I
recently started studying apk, so there are a lot of stupid questions
5df35e3985e6e707622525.png
. When you launch the application, the questions disappear.

public void parseJson(String jsonData) {
        try {

            JSONObject jsonObjMain = new JSONObject(jsonData);
            JSONArray jsonArray = jsonObjMain.getJSONArray(AppConstants.JSON_KEY_QUESTIONNAIRY);

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObj = jsonArray.getJSONObject(i);

                //String picture = jsonObj.getString(AppConstants.JSON_KEY_PICTURE);
                String question = jsonObj.getString(AppConstants.JSON_KEY_QUESTION);
                int correctAnswer = Integer.parseInt(jsonObj.getString(AppConstants.JSON_KEY_CORRECT_ANS));
                String categoryId = jsonObj.getString(AppConstants.JSON_KEY_CATEGORY_ID);


                Log.d("TAG", categoryId.toString());

                JSONArray jsonArray2 = jsonObj.getJSONArray(AppConstants.JSON_KEY_ANSWERS);
                ArrayList<String> contents = new ArrayList<>();
                ArrayList<String> backgroundColors = new ArrayList<>();
                for (int j = 0; j < jsonArray2.length(); j++) {
                    String item_title = jsonArray2.get(j).toString();
                    contents.add(item_title);
                    backgroundColors.add(AppConstants.COLOR_WHITE);
                }
                if (mCategoryId.equals(categoryId)) {
                    mItemList.add(new QuizModel(question, contents, correctAnswer, categoryId, backgroundColors));
                }
            }

            mQuestionsCount = mItemList.size();
            Collections.shuffle(mItemList);

            hideLoader();
            updateQuestionsAndAnswers();

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

This line kills //String picture = jsonObj.getString(AppConstants.JSON_KEY_PICTURE); if you uncomment it

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question