Code snippet to save/fetch data from file system

To save data from file system you need to add write access storage system permission in application’s manifest.

You can write data in internal as well as on external sd card.

Code to write data on disc


 protected static void saveDataOnDisk(String data) {

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

try {

ObjectOutput objectOutput = new ObjectOutputStream(byteArrayOutputStream);

objectOutput.writeObject(data);

byte[] buffer = byteArrayOutputStream.toByteArray();

File loginDataFile = (new File(filePath)); // file path where you want to write your data

loginDataFile.createNewFile();

FileOutputStream fileOutputStream = new FileOutputStream(loginDataFile);

fileOutputStream.write(buffer);

fileOutputStream.close();

objectOutput.flush();

objectOutput.close();

byteArrayOutputStream.flush();

byteArrayOutputStream.close();

Log.i(“SAVE”, ”———————-DONE SAVING”);

} catch(IOException ioe) {

Log.i(“SAVE”, “———serializeObject|”+ioe);

}

}

Here is code snippet to save bitmap data on file system


File file = new File(filePath); // file path where you want to // save data.

FileOutputStream fileOutputStream = new FileOutputStream(file);

bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream); //bitmap is Bitmap object which you want to save on disc.

fileOutputStream.flush(); fileOutputStream.close();

code to fetch Data From disc

private static Object getDataFromDisk() {

try {

FileInputStream fileInputeStream = new FileInputStream(FilePath);

ObjectInputStream objectInputStream = new ObjectInputStream(fileInputeStream);

Object data = (Object) objectInputStream.readObject();

objectInputStream.close();

fileInputeStream.close();

return dataModel;

} catch (Exception error) {

Log.i(“FETCH”, ”—-getDataFromDisk———ERROR while reading|” + error);

}

return null;

}

Advertisements

Gmail like pull to refresh

Hey  All Here is our new library which has gmail like refresh functionality.

It’s a simple & fully customizable library  which you can use  in your app easily

You can download library & sample project from github.

gmail like pull to refresh

gmail like pull to refresh

*Steps for usage.

1] Here is the code snippet


CustomView view = new CustomView(getApplicationContext(), actionBar);

setContentView(view);

view.setRefreshListner(SampleForGmailLIkePullToRefresh.this);

view.setActionBar(SampleForGmailLIkePullToRefresh.this);

view.getListView().setAdapter(new DummyAdapter(this)); //set adapter to list.

You can implement IRefreshListner interface which has two methods preRefresh() postRefresh() which will be invoked when loading starts and stops respectively.

You can use these methods to write logic which will be executed before and after refreshing.

Little more about CustomView class

We have exposed following  useful methods of CustomView class.

  • startLoading()  to start refreshing/loading explicitly.
  • stopLoading()  to stop refreshing/loading explicitly.
  • getListView()  this will return listview.

Its an Apache License, Version 2.0 library so you can use it in your apps the way you want. Let us know in case of any issues. Download this library from  github.

Happy Coding Happy Learning 🙂