0

I want to download images from server using json and storing it in internal storage , after that show image one by one. I have the JSON format can any body help me with how to do it??? My json is:

    {
"_id" : ObjectId("55b09029e56e5ecc1577f00e"),
"user" : ObjectId("559a298d9969172f3ffeaa6d"),

"name" : "ddd", 
"language" : "english",


"pages" :

[ {

"_id" : ObjectId("55b0902be56e5ecc1577f00f"),


"page_image" : [

{ "image" : "images.png", "_id" : ObjectId("55b09032e56e5ecc1577f010") }, 
{ "image" : "20140624_172041_fbhciha_sm.jpeg", "_id" : ObjectId("55b0903ce56e5ecc1577f011") }, 
{ "image" : "673.jpg", "_id" : ObjectId("55b09042e56e5ecc1577f012") } ] } ], "__v" : 4 }
Chinmoy Debnath
  • 2,814
  • 16
  • 20
Hassan Hameed
  • 13
  • 1
  • 7
  • I would suggest to learn json parsing first: http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ Once you will under stand it and able to fetch image url from your response then use @streamride approach to show your image to any view. – Adarsh Yadav Jul 23 '15 at 12:27
  • sure, it would be good – Hassan Hameed Jul 23 '15 at 12:31
  • :- Once you will finish above I would suggest to use Picasso( http://square.github.io/picasso/) for Image downloading All work starting from download image from url to bitmap conversion and set to image view will be handle by Picasso. For example : Picasso.with(context).load(url).into(view/*Your image view*/); – Adarsh Yadav Jul 23 '15 at 12:36
  • fine but i need to store the downloaded image first into device then i need to show it to the View??? – Hassan Hameed Jul 23 '15 at 12:41
  • Follow this this tutorial (http://androidappmasters.com/forum/viewtopic.php?t=15) once you will start using Picasso, In the last section you will find the way to store your downloaded image to you device. – Adarsh Yadav Jul 23 '15 at 12:48
  • I am done with getting the url from the json now i need to download images from these url and need to store them in internal storage ?? help please how can i do that ?? @AdarshYadav – Hassan Hameed Jul 25 '15 at 13:56
  • :- Sorry for late response please refer my answer below. Hope it will help you. Cheers !!! – Adarsh Yadav Jul 26 '15 at 10:11

3 Answers3

0

One of the simplest way is to use Glide library (https://github.com/bumptech/glide) you can get bitmap as

Bitmap bitmap = Glide.with(pContext).load(url).asBitmap().into(100, 100).get();

and then save bitmap

be sure that you call that method in new thread

streamride
  • 567
  • 4
  • 16
  • but i need to parse this json first ? isn't it ?? i need to ask that how to parse it? – Hassan Hameed Jul 23 '15 at 11:18
  • @HassanHameed ok, you can do like this - JSONObject obj = new JSONObject(); then, JSONArray pageImages = obj.getJSONArray("page_images"); and for( int i=0; i < pageImages.length(); i++) { JSONObject image = pageImages.get(i); String url = image.getString("image"); } – streamride Jul 23 '15 at 11:23
  • @HassanHameed sorry, I missed JSONArray named pages, you need to parse like I posted above – streamride Jul 23 '15 at 11:24
  • this is a nested array first i have array of pages then inside it there is array of page images.. i want to make it dynamic in response to the server request it will give me json, how to impement that json ?? – Hassan Hameed Jul 23 '15 at 11:43
  • @HassanHameed I posted as answer how to parse it – streamride Jul 23 '15 at 11:52
  • i have a download button, i want when i click it and it sends request to server and in response a json will be generated in format that i have posted above ?? how to send request o server ? on button click – Hassan Hameed Jul 23 '15 at 12:00
0

You get String in response to server. Then you create json object

String strResponse = ...
JSONObject jsonObject = new JSONObject(strResponse);
JSONArray pagesArray = jsonObject.getJSONArray("pages");
for (int i=0;i<pagesArray.length();i++){
  JSONObject pageObj = pagesArray.getJSONObject(i);
  JSONArray pageImagesArray = pageObj.getJSONArray("page_image");
  for (int j=0;j<pageImagesArray.length();j++) {
     JSONObject image = pageImagesArray.getJSONObject(j);
      String urlImg = image.getString("image");
  }
}
streamride
  • 567
  • 4
  • 16
  • in response of the server i am not getting string i am getting the json in the format i have posted in my question ??? – Hassan Hameed Jul 23 '15 at 11:56
  • sorry i could not understand .. which two lines and where to pass ? – Hassan Hameed Jul 23 '15 at 12:06
  • i am having a download button on click it will send request to server and in response i will get json and then i have to parse it, i am wondering how to do this from download request to parsing this json ?? and saving images to device ??? – Hassan Hameed Jul 23 '15 at 12:07
  • you said you get json format, is that String object or JSONObject object? – streamride Jul 23 '15 at 12:10
  • So, if the name of your jsonObject is "jsonObject" you start from this line to parsing it (JSONArray pagesArray = jsonObject.getJSONArray("pages");) – streamride Jul 23 '15 at 12:19
  • alright this is good, i can get the string of the image, now if i want to download the images from the server and to store it in internal storage ?? – Hassan Hameed Jul 23 '15 at 12:26
  • then when you get Bitmap object (as I posted) you can save it like described here http://stackoverflow.com/a/15662384/1216446 – streamride Jul 23 '15 at 12:30
  • i have got the url of the image from the json.. now i need to store those images into my internal disk and show them on a read button ??? – Hassan Hameed Jul 25 '15 at 09:09
  • I am done with getting the url from the json now i need to download images from these url and need to store them in internal storage ?? help please how can i do that ?? @streamride – Hassan Hameed Jul 25 '15 at 13:57
  • @HassanHameed first read that http://stackoverflow.com/a/31585778/1216446 and then http://stackoverflow.com/questions/15662258/how-to-save-a-bitmap-on-internal-storage/15662384#15662384 – streamride Jul 25 '15 at 21:13
0

1- Add compile 'com.squareup.picasso:picasso:2.5.2'inside build.gradle or download jar file from Picasso

2- Picasso.with(this) .load("Your image url here") .into(target); If you list then you can also use above during traversing your list(inside any loop).

3-As target in above class displaying compile time error. Put following code as your inner class.

private Target target = new Target() {
@Override
 public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
  new Thread(new Runnable() {
       @Override
     public void run() {

        File file = new File(Environment.getExternalStorageDirectory().getPath() + "/image1.jpg");
        try {
           file.createNewFile();
           FileOutputStream ostream = new FileOutputStream(file);
           bitmap.compress(CompressFormat.JPEG, 80, ostream);
           ostream.close();
        } catch (Exception e) {
           e.printStackTrace();
        }}
  }).start(); }
   @Override
    public void onBitmapFailed(Drawable errorDrawable) {}
   @Override
   public void onPrepareLoad(Drawable placeHolderDrawable) {
  if (placeHolderDrawable != null) {} }};

4-If you are still facing problem in understanding above code please refer following example. Picasso - Image downloading and caching library for Android

5- ` for(int i = 0; i < cons.length(); i++)

{
    JSONObject c = cons.getJSONObject(i);
    imgURL = c.getString(image);
    Picasso.with(this)
            .load(imgURL)
            .into(new Target() {
                @Override
                public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {

                            File file = new File(Environment.getExternalStorageDirectory().getPath() + "/image1.jpg");
                            try {
                                file.createNewFile();
                                FileOutputStream ostream = new FileOutputStream(file);
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
                                ostream.close();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }}
                    }).start();
                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {

                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            });
}`
Adarsh Yadav
  • 3,752
  • 3
  • 24
  • 46