0

How to save full ListView Data (which i fetch from Internet) contained (bitmap and strings) to internal memory for Future Use when Internet is not Available.
This is My Main Code:DisplayList.java

    public class DisplayList extends ListActivity {

    List<Flower> flowerList;
    ProgressBar progressBar;
    List<GetData> task;
    public static final String PHOTO_BASE_URL="http://services.hanselandpetal.com/photos/";
    private static final int REQUEST_CODE = 100;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_list);
      //  textView=(TextView)findViewById(R.id.data);
      //  textView.setMovementMethod(new ScrollingMovementMethod());

        progressBar=(ProgressBar)findViewById(R.id.progressBar);
        progressBar.setVisibility(View.INVISIBLE);
        task=new ArrayList<>();


        if(isOnline())
        {
            requestData("http://services.hanselandpetal.com/feeds/flowers.json");
        }
        else
        {
            Toast.makeText(DisplayList.this, "Network is not Available", Toast.LENGTH_LONG).show();
        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_display_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

//        if (id == R.id.task1)
//        {
//
//        }


        return super.onOptionsItemSelected(item);
    }

    private void requestData(String url) {
        GetData getData=new GetData();
        getData.execute(url);
    }


    public void update()
    {
        //Use FlowerAdapter to display data
        FlowerAdapter adapter = new FlowerAdapter(this, R.layout.item_flower, flowerList);
        setListAdapter(adapter);

    }

    protected boolean isOnline()
    {
        ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
        if(networkInfo!=null && networkInfo.isConnectedOrConnecting())
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    private class GetData extends AsyncTask<String,String,List<Flower>>
    {
        @Override
        protected void onPreExecute()
        {
            // update("Task Started");
            if(task.size()==0) {
                progressBar.setVisibility(View.VISIBLE);
            }
            task.add(this);
        }

        @Override
        protected List<Flower> doInBackground(String... params) {
            String content="Internet is to slow";
            content=HttpManager1.getDataByHttpUrlConnection(params[0]);
            flowerList= FlowerJSONParser.parseFeed(content);




//            for(Flower flower:flowerList)
//            {
//                try
//                {
//                    String imageUrl=PHOTO_BASE_URL+flower.getPhoto();
//                    InputStream inputStream=(InputStream)new URL(imageUrl).getContent();
//                    Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
//                    flower.setBitmap(bitmap);
//                    inputStream.close();
//                }
//                catch (Exception e)
//                {
//                    e.printStackTrace();
//                }
//
//            }


            return flowerList;
        }

        @Override
        public void onPostExecute(List<Flower> result)
        {

            update();

            task.remove(this);
            if(task.size()==0) {
                progressBar.setVisibility(View.INVISIBLE);
            }


        }

        @Override
        protected void onProgressUpdate(String... values) {
            //update(values[0]);
        }
    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Flower flower=flowerList.get(position);
        Intent intent=new Intent(this,DetailActivity.class);
        intent.putExtra("flowerName",flower.getName());
        intent.putExtra("imageBitmap",flower.getBitmap());
        intent.putExtra("instruction",flower.getInstructions());
        startActivityForResult(intent, REQUEST_CODE);

    }

}

</pre>

In the above Program I am fetching flowerList (which contained bitmaps and Strings) from server in doInBackground-Method and pass it into FlowerAdapter to set ListView Items. Can you please give me a way to stored all bitmap images and Strings(contained names and description) to Internal Memory.If you take this example and solve my big problem then i will be very appreciable to you.

DRY Believer
  • 1,001
  • 11
  • 20
  • create database for this purpose.insert text and images in this database and when you are offline pull them from database and use. – DkThakur Mar 31 '17 at 10:47
  • here is the link to do so http://stackoverflow.com/questions/11790104/how-to-storebitmap-image-and-retrieve-image-from-sqlite-database-in-android – DkThakur Mar 31 '17 at 10:54

2 Answers2

0

You just create a databse for local storage where just create a table with column name (id,flowerName,imageBitmap,instruction) and what ever you want to add after creating table just insert your data in localDB and when ever you need just fetch from there.

for creating database just make a class which extends SQLiteOpenHelper and in that implement onCreate and onUpgrade method

in oncreate you just execute your table creation query.

Thanks!

Nikhil Sharma
  • 593
  • 7
  • 23
0

http://www.vogella.com/tutorials/AndroidSQLite/article.html

Please read the above tutorial for working with sqlite .The main issue of saving data would be resolved when there is no internet connection.Other that that use image library like Picasso or Fresco to save image in your local memmory.

DRY Believer
  • 1,001
  • 11
  • 20
  • why image in local memory,image also can be stored in database. – DkThakur Mar 31 '17 at 10:52
  • Its better not to store it in blob format .This might reduce the quality of image and increase the size of your db .Not a good solution to store images in Db and neither its a good pratice .Please read below http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – DRY Believer Mar 31 '17 at 10:53