0

Hello I am writing an Application in which i am parsing JSON Images and then caching into SD Card.

What I want to do ?

I want to load images into GridView from JSON (by caching images into SD Card), and wanna populate GridView (no matter Internet available or not) once images already downloaded into SD Card.

What I am getting ?

I am able to cache images into SD Card, also to populate GridView, but not able to show images into GridView (if Internet not available) but images cached into SD Card

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view);

        gridView = (GridView) findViewById(R.id.grid_view);

        utils = new Util(this);

        // Initilizing Grid View
        InitilizeGridLayout();
        // Gridview adapter
        new getImages().execute();
    }

    private void InitilizeGridLayout() {
        Resources r = getResources();
        float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                AppConstant.GRID_PADDING, r.getDisplayMetrics());

        columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);

        gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
        gridView.setColumnWidth(columnWidth);
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setPadding((int) padding, (int) padding, (int) padding,
                (int) padding);
        gridView.setHorizontalSpacing((int) padding);
        gridView.setVerticalSpacing((int) padding);
    }
    class getImages extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            JSONObject json = JSONfunctions.getJSONfromURL(location);

            try {
                JSONArray jarray;
                jarray = json.getJSONArray(TAG_ITEMS);
                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject gridImages = jarray.getJSONObject(i);
                    imagePaths.add(gridImages.getString("saved_location"));
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            adapter = new GridViewImageAdapter(GridViewActivity.this, imagePaths,
                    columnWidth);

            // setting grid view adapter
            gridView.setAdapter(adapter);
        }
    }
}
Sophie
  • 2,594
  • 10
  • 41
  • 75
  • Its very easy, just grab images from sd card and display it in gridview. You are doing it using url, if internet is not available then how it will get image from server. So this will not work, you must need to grab t from SD Card. – Pratik Dasa Jun 03 '14 at 09:07
  • what if i have categorised images, like Event-A, Event-B and all going caching into LazyList folder into SDCard ? @pratik – Sophie Jun 03 '14 at 09:28
  • Sophie, have you done with this or not? – Pratik Dasa Jun 05 '14 at 11:16

3 Answers3

2

Its better you use Lazyloading.. this will cache the images and display images even there is no INTERNET is available

You can either use thest1/LazyList

or Advance image caching Android-Universal-Image-Loader From here

Android-Universal-Image-Loader contain different examples including Lazyloding in Grid view.

Hope this will help you

Ramz
  • 7,116
  • 6
  • 63
  • 88
2

Here is the complete solution of Viewing images from server via JSON online as well offline too..

  1. DbAdapter

  2. GridViewActivity

  3. FullScreenViewActivity

Hoping it will solve your problem.

Thanks

PankajSharma
  • 1,529
  • 14
  • 27
0

i think this may help you. Just put another if block before checking internet connection

 if(SDcard is emty)
 {
   if(netAvailable)
     {go as usual}
    }else{nothing}
 }
 else{
     take images from sd card and display it.}

this might give you some hint to solve the problem...... if you want to refresh your sd card data then before hitting the web service clear the sdcard.

Mehboob Maniyar
  • 238
  • 1
  • 3
  • 11