如何在android中存储图像和名称

时间:2015-12-04 07:42:37

标签: android

我有一个带有图像和类别名称的GirdView。我从服务器获取名称和图像。我使用Universal Image Loader加载图像。我希望当用户打开应用程序时,它应该加载图像和category_name一次并保存它,以便用户可以直接查看图像和category_name而无需加载。这是我的MainActivity

<my-panel some-text="helloText"></my-Panel>

这个CategoriesAdapter

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    al_city = new ArrayList<Pojo>();
    al_category = new ArrayList<Pojo>();

    //Grid View
    gridview = (GridView) findViewById(R.id.gridview);

    new NetCheck().execute();
}



private class NetCheck extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        nDialog = new ProgressDialog(MainActivity.this);
        nDialog.setMessage("Loading..");
        nDialog.setTitle("Please Wait");
        nDialog.setIndeterminate(false);
        nDialog.setCancelable(true);
        nDialog.show();
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        nDialog.dismiss();
        adapter = new CategoriesAdapter(MainActivity.this, al_category);
        gridview.setAdapter(adapter);
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        try {
          HttpClient httpClient = new DefaultHttpClient();
          String url = Config.CITIESCATEGORIES;
              HttpPost httpRequest = new HttpPost(url);
              httpRequest.setHeader("Content-Type", "application/json");

              JSONObject json = new JSONObject();

              Log.e("JSON Object", json.toString());

              StringEntity se = new StringEntity(json.toString());
              se.setContentEncoding("UTF-8");
              se.setContentType("application/json");
              httpRequest.setEntity(se);
              HttpResponse httpRes = httpClient.execute(httpRequest);
              java.io.InputStream inputStream = httpRes.getEntity()
                    .getContent();
              InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream);
              BufferedReader reader = new BufferedReader(inputStreamReader);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
              }
              inputStream.close();
              strServerResponse = sb.toString();
              Log.e("Server Response", "" + strServerResponse.toString());
              if (strServerResponse != null) {

              JSONArray arr = new JSONArray(strServerResponse);
              for (int i = 0; i<arr.length(); i++){
                    JSONObject jobj2 = arr.getJSONObject(i);
                    objCity = jobj2.getString("city");
                    objCategory = jobj2.getString("category");
                    imagePath = jobj2.getString("cat_img_path");

                }

                JSONArray gg = new JSONArray(objCity);
                for (int j=0; j<gg.length(); j++){
                    pojo = new Pojo();
                    JSONObject jobj = gg.getJSONObject(j);
                    String cityid = jobj.optString("id");
                    String cityName = jobj.optString("name");
                    pojo.setCityId(cityid);
                    pojo.setCityName(cityName);
                    al_city.add(pojo);
                }

                JSONArray ff = new JSONArray(objCategory);
                for (int j=0; j<ff.length(); j++){
                    pojo = new Pojo();
                    JSONObject jobj3 = ff.getJSONObject(j);
                    String catId = jobj3.optString("id");
                    String imagename = jobj3.optString("image");
                    String catname = jobj3.optString("name");

                    pojo.setCategoryId(catId);
                    pojo.setCatImage(imagename);
                    pojo.setCatName(catname);
                    al_category.add(pojo);
                }

            } else {
               Log.e("ServiceHandler",
                        "Couldn't get any data from the url");
           } 
   }

请帮帮我。怎么做。

2 个答案:

答案 0 :(得分:0)

您可以使用SharedPreferences存储键值字符串值,也可以将值存储在SQLite数据库中。您可以查看更多选项here

答案 1 :(得分:0)

我总是使用 SharedPreferences。 例如: 我想存储一个图像。 Yu添加int j; (sharedpreferences)如果你的值j等于1,那么使用你的SharedPreferences&#39; s并存储它。 与sharedPreferences字符串相同。 你有explanation here