Qandeel Haider
Qandeel Haider

Reputation: 97

Get image from server and sets to int array in Android

I want to take image in integer Array just like when you take R.drawable.img in integer array.

e.g : int[] images = {R.drawable.a , R.drawable.b , R.drawable.c , R.drawable.d};

I want same like this when you take image from server.

Declaration of Array:

                  String images[] = new String[1000];

Get Image from server and sets it to int array which is not happening.

            public void onResponse(String response) {
            Log.d("TAG", "Message Response: " + response.toString());
            hideDialog();

            try {
                JSONObject jsonObj = new JSONObject(response);
                boolean error = false;
                p = jsonObj.getJSONArray("response");

                for (int i = 0 ; i < p.length(); i++) {

                    JSONObject c = p.getJSONObject(i);
                    String profilepicture = c.getString("profile_picture");

                    byte[] decodedString = Base64.decode(profilepicture, Base64.DEFAULT);
                    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);


                    Drawable drawable = new BitmapDrawable(getResources(),decodedByte);

                    images[i] = drawable;

}

The drawable values is android.graphics.drawable.BitmapDrawable@269a3b6d. But it not sets on the integer array.

Upvotes: 0

Views: 555

Answers (2)

Wajid
Wajid

Reputation: 2341

create an array for drawables then it would work, and you can get the image from that array via its integer index. but the case you are trying is that, you would have got that number BitmapDrawable@269a3b6d from debugging the code? right? this is a reference to the object in the memorey and not an integer! so don't try to put it in the integer array. now either make your array type to drawable or change your scenario!

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93624

You're getting a Drawable. You can't save a Drawable to an integer array or a String array- you have to make it a Drawable array. This approach is really NOT recommended btw- you're storing all of your images in memory, which will take a lot of memory. If its a non-trivial number you'll likely hit an OOM exception. For that matter, sending down all these images as Base64 encoded strings is not recommended either. The better way to do things is to have the server send down a URL of the image to download, and to download it only when you're sure you need it.

Upvotes: 2

Related Questions