Android Open Source - android-weather-demo-application Resource Helper






From Project

Back to project page android-weather-demo-application.

License

The source code is released under:

GNU General Public License

If you think the Android project android-weather-demo-application listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.openweathermap.utils;
//from w  w w. j  a  va 2s  . com
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;

/**
 * Helper methods for managing resources
 * @author samkirton
 */
public class ResourceHelper {
  
  /**
   * Retrieves a text file resource from /raw and reads it into memory as a String
   * @param  context  The application context
   * @param  resId  The resourceId of the file being read into memory
   * @return  The string contents of the file
   */
  public static String getFileContentsAsString(Context context, int resId) {
    String fileContents = null;
    
    InputStream inputStream = context.getResources().openRawResource(resId);
    InputStreamReader inputReader = new InputStreamReader(inputStream);
    BufferedReader bufferedReader = new BufferedReader(inputReader);
    
    StringBuilder stringBuilder = new StringBuilder();
    
      try {
        String line;
          while ((line = bufferedReader.readLine()) != null) {
              stringBuilder.append(line);
          }
      } catch (IOException e) { 
        
      } finally {
        closeInputStream(inputStream);
        closeInputStreamReader(inputReader);
        closeBufferedReader(bufferedReader);
      }
    
      fileContents = stringBuilder.toString();
    return fileContents;
  }
  
  /**
   * Close the inputStream associated with the provided reference
   * @param  inputStream  The reference of the inputStream to close
   */
  private static void closeInputStream(InputStream inputStream) {
    try {
      inputStream.close();
    } catch (IOException e) { }
  }
  
  /**
   * Close the inputStreamReader associated with the provided reference
   * @param  inputStreamReader  The reference of the inputStreamReader to close
   */
  private static void closeInputStreamReader(InputStreamReader inputStreamReader) {
    try {
      inputStreamReader.close();
    } catch (IOException e) { }
  }
  
  /**
   * Close the bufferedReader associated with the provided reference
   * @param  bufferedReader  The reference of the bufferedReader to close
   */
  private static void closeBufferedReader(BufferedReader bufferedReader) {
    try {
      bufferedReader.close();
    } catch (IOException e) { }
  }
}




Java Source Code List

org.openweathermap.WeatherApplication.java
org.openweathermap.activity.MainActivity.java
org.openweathermap.activity.base.BaseActivity.java
org.openweathermap.adapter.CityAdapter.java
org.openweathermap.adapter.ForecastAdapter.java
org.openweathermap.asynctask.GetCitiesAsyncTask.java
org.openweathermap.asynctask.GetLastCityAsyncTask.java
org.openweathermap.asynctask.SaveForecastAsyncTask.java
org.openweathermap.asynctask.SearchCitiesAsyncTask.java
org.openweathermap.asynctask.base.BaseAsyncTask.java
org.openweathermap.asynctask.base.BaseResponse.java
org.openweathermap.asynctask.base.IParam.java
org.openweathermap.asynctask.param.SaveForecastParam.java
org.openweathermap.asynctask.param.SearchCitiesParam.java
org.openweathermap.asynctask.response.GetCitiesResponse.java
org.openweathermap.asynctask.response.GetLastCityResponse.java
org.openweathermap.asynctask.response.SaveForecastResponse.java
org.openweathermap.dto.CityDTO.java
org.openweathermap.dto.CoordDTO.java
org.openweathermap.dto.DayDTO.java
org.openweathermap.dto.ResultDTO.java
org.openweathermap.dto.TempDTO.java
org.openweathermap.dto.WeatherDTO.java
org.openweathermap.dto.base.IDTO.java
org.openweathermap.fragment.ForecastFragment.java
org.openweathermap.fragment.ModalDialogFragment.java
org.openweathermap.fragment.SelectCountryFragment.java
org.openweathermap.sql.SQLInitProvider.java
org.openweathermap.sql.model.CityModel.java
org.openweathermap.sql.model.IconModel.java
org.openweathermap.sql.model.WeatherModel.java
org.openweathermap.utils.DateHelper.java
org.openweathermap.utils.RESTProvider.java
org.openweathermap.utils.ReflectionHelper.java
org.openweathermap.utils.ResourceHelper.java
org.openweathermap.utils.StringHelper.java
org.openweathermap.view.CityListItemView.java
org.openweathermap.view.DateDisplayView.java
org.openweathermap.view.ViewPagerFade.java
org.openweathermap.view.ViewPagerIndicatorView.java
org.openweathermap.view.WeatherDataRowView.java
org.openweathermap.volley.callback.VolleyResponseCallback.java
org.openweathermap.volley.provider.VolleyImageLoader.java
org.openweathermap.volley.provider.VolleyRequest.java