How to populate GridView if Internet not available but images already cached to SD Card
-
21-12-2019 - |
Question
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);
}
}
}
Solution
Here is the complete solution of Viewing images from server via JSON online as well offline too..
Hoping it will solve your problem.
Thanks
OTHER TIPS
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
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.