AndroidF
AndroidF

Reputation: 125

Android JSON parsing: how to send image with intent

I'm using json for creating list of items. When user click it opens another screen with some detail about that item. I have problem with sending image to that activity. This is my code for json parsing:

for (int i = 0; i < data.length(); i++) {
        JSONObject c = data.getJSONObject(i);

        String id = c.getString(TAG_ID);
        String name = c.getString(TAG_NAME);
        String link_image = c.getString(TAG_LINK_IMAGE);        

        HashMap<String, String> map = new HashMap<String, String>();

        map.put(TAG_ID_RS, id);
        map.put(TAG_NAME, name);
        map.put(TAG_LINK_IMAGE, link_image);

        Data.add(map);
}

and this is for setOnItemClickListener:

public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    String name_two = ((TextView) view
                            .findViewById(R.id.name)).getText()
                            .toString();

                    Intent i = new Intent(getApplicationContext(),
                    SingleItem.class);

                    i.putExtra("name", name_two);

                    startActivity(i);

                }

In case of sending string "name" i am locating TextView and pulling string out of it, but what about image? Thanks.

Upvotes: 0

Views: 701

Answers (2)

mawalker
mawalker

Reputation: 2070

Intent bundles aren't meant for passing large amounts of data. They are best used for small amounts of text, such as URIs, or something similar.

Without knowing where the images are coming from, I'm going to guess that they are images saved somewhere within your application's /res or cache directory.

If this is true, I'd say the best bet (if these activities are within the same application) is to just pass along the location of where the image is saved inside the Intent. That way you avoid encoding/decoding of the image through the Intent.

If the two activities are in separate applications. Then this is almost the canonical use case for a Content Provider.

Here are two other questions that are trying to do a similar thing Intent.putExtras size limit? and Maximum length of Intent putExtra method? (Force close) , and pass data in an intent. Both ended up with system hanging if you put too much data. I highly recommend NOT using the Application class as a data storage mechanism solution the first one recommends. Very bad Android design, and can lead to emergent errors down the road. Shared Preferences (for application wide settings) or Content Provider/local cached files for file transfer is a much more robust answer.

Upvotes: 1

Ercan
Ercan

Reputation: 3705

You have int position and you have a collection called Data

so you can use:

String link =  Data.get(position).get(TAG_LINK_IMAGE);

Upvotes: 0

Related Questions