Android 如果Internet不可用,但图像已缓存到SD卡,如何填充GridView
您好,我正在编写一个应用程序,其中我正在解析JSON图像,然后缓存到SD卡中 我想做什么? 我想将图像从JSON加载到GridView中(通过将图像缓存到SD卡),并想在图像下载到SD卡后填充GridView(无论互联网是否可用) 我得到了什么? 我可以将图像缓存到SD卡中,也可以填充GridView,但不能将图像显示到GridView中(如果Internet不可用),而是将图像缓存到SD卡中Android 如果Internet不可用,但图像已缓存到SD卡,如何填充GridView,android,json,gridview,image-caching,Android,Json,Gridview,Image Caching,您好,我正在编写一个应用程序,其中我正在解析JSON图像,然后缓存到SD卡中 我想做什么? 我想将图像从JSON加载到GridView中(通过将图像缓存到SD卡),并想在图像下载到SD卡后填充GridView(无论互联网是否可用) 我得到了什么? 我可以将图像缓存到SD卡中,也可以填充GridView,但不能将图像显示到GridView中(如果Internet不可用),而是将图像缓存到SD卡中 @Override protected void onCreate(Bundle sa
@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);
}
}
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u grid\u视图);
gridView=(gridView)findViewById(R.id.grid\u视图);
utils=新的Util(本);
//初始化网格视图
InitilizeGridLayout();
//Gridview适配器
新建getImages().execute();
}
私有void InitilizeGridLayout(){
Resources r=getResources();
浮动填充=TypedValue.applyDimension(TypedValue.COMPLEX\u UNIT\u DIP,
AppConstant.GRID_PADDING,r.getDisplayMetrics();
columnWidth=(int)((utils.getScreenWidth()-((AppConstant.NUM_OF_COLUMNS+1)*padding))/AppConstant.NUM_OF_COLUMNS);
setNumColumns(AppConstant.NUM\u OF_列);
gridView.setColumnWidth(columnWidth);
gridView.setStretchMode(gridView.NO_STRETCH);
setPadding((int)padding,(int)padding,(int)padding,
(int)填充);
setHorizontalSpacing((int)padding);
设置垂直间距((int)填充);
}
类getImages扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
JSONObject json=JSONfunctions.getJSONfromURL(位置);
试一试{
杰索纳雷·贾雷;
jarray=json.getJSONArray(标记项);
for(int i=0;i
你最好用懒散的方式。。这将缓存图像并显示图像,即使没有可用的INTERNET
你可以使用
或高级图像缓存Android通用图像加载器
Android通用图像加载器包含不同的示例,包括网格视图中的懒散
希望这能帮助你你最好用懒散的方式。。这将缓存图像并显示图像,即使没有可用的INTERNET 你可以使用 或高级图像缓存Android通用图像加载器 Android通用图像加载器包含不同的示例,包括网格视图中的懒散
希望这会对你有所帮助我想这可能会对你有所帮助。只需在检查internet连接之前放置另一个if块
if(SDcard is emty)
{
if(netAvailable)
{go as usual}
}else{nothing}
}
else{
take images from sd card and display it.}
这可能会给你一些解决问题的提示。。。。。。
如果要刷新sd卡数据,请在点击web服务之前清除sd卡。我认为这可能会对您有所帮助。只需在检查internet连接之前放置另一个if块
if(SDcard is emty)
{
if(netAvailable)
{go as usual}
}else{nothing}
}
else{
take images from sd card and display it.}
这可能会给你一些解决问题的提示。。。。。。
如果要刷新sd卡数据,请在点击web服务之前清除sd卡。以下是通过JSON在线和离线查看服务器图像的完整解决方案 希望它能解决你的问题
谢谢这是通过JSON在线和离线查看服务器图像的完整解决方案 希望它能解决你的问题
谢谢非常简单,只需从sd卡抓取图像并在gridview中显示即可。你们是用url做的,若互联网不可用,那个么它将如何从服务器获取图像。所以,这将不起作用,你们必须从SD卡中获取t。若我将图像分类,比如事件A、事件B和所有将缓存到LazyList文件夹的SD卡中,会怎么样@pratikSophie,你完成了吗?这很简单,只需从sd卡抓取图像并在gridview中显示即可。你们是用url做的,若互联网不可用,那个么它将如何从服务器获取图像。所以,这将不起作用,你们必须从SD卡中获取t。若我将图像分类,比如事件A、事件B和所有将缓存到LazyList文件夹的SD卡中,会怎么样@pratikSophie,你有没有用过这个?我用的是Android通用图像加载器我用的是Android通用图像加载器