Example usage for android.content Context openFileInput

List of usage examples for android.content Context openFileInput

Introduction

In this page you can find the example usage for android.content Context openFileInput.

Prototype

public abstract FileInputStream openFileInput(String name) throws FileNotFoundException;

Source Link

Document

Open a private file associated with this Context's application package for reading.

Usage

From source file:org.montanafoodhub.base.get.AdHub.java

protected List<Ad> readFromFile(Context context) {
    List<Ad> myAdArr = new ArrayList<Ad>();
    try {//from   w  w w.java 2  s .  c om
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myAdArr, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of Ad loaded: " + myAdArr.size());
    return myAdArr;
}

From source file:com.agateau.equiv.ui.Kernel.java

private void loadProductList(Context context) {
    InputStream stream;//from w w  w .  j a  va2  s  .  c  o  m
    mProductStore = new ProductStore();
    try {
        stream = context.getAssets().open("products.csv");
    } catch (IOException e) {
        throw new RuntimeException("Failed to open products.csv.", e);
    }
    try {
        ProductStoreCsvIO.read(stream, mProductStore, Product.Source.DEFAULT);
    } catch (IOException e) {
        throw new RuntimeException("Failed to read products.csv.", e);
    }

    try {
        stream = context.openFileInput(CUSTOM_PRODUCTS_CSV);
    } catch (FileNotFoundException e) {
        return;
    }
    try {
        ProductStoreCsvIO.read(stream, mProductStore, Product.Source.CUSTOM);
    } catch (IOException e) {
        // Do not throw an exception here, the app is still usable even if we failed to load custom products
        NLog.e("Failed to read custom products from %s: %s.", CUSTOM_PRODUCTS_CSV, e);
    }
}

From source file:org.montanafoodhub.base.get.OrderHub.java

protected List<Order> readFromFile(Context context) {
    List<Order> myOrderArr = new ArrayList<Order>();
    try {//from   w w  w .  j a va  2 s  .  com
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myOrderArr, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    } catch (ParseException pe) {
        Log.e(HubInit.logTag, "Can't parse Order date  (" + fileName + ") : " + pe.toString());
    }
    Log.w(HubInit.logTag, "Number of orders loaded: " + myOrderArr.size());
    return myOrderArr;
}

From source file:org.montanafoodhub.base.get.InitHub.java

protected void readFromFile(Context context) {
    try {/* w  w w  . ja  va  2s.c  o m*/
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // *** last line in the init wins!!!
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Hub name initialized to: " + HubInit.getHubName());
}

From source file:org.montanafoodhub.base.get.ItemHub.java

protected HashMap<String, Item> readFromFile(Context context) {
    HashMap<String, Item> myItemMap = new HashMap<String, Item>();
    try {//w w w .  ja  v  a 2 s  .  c  om
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myItemMap, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of items loaded: " + myItemMap.size());
    return myItemMap;
}

From source file:br.com.carlosrafaelgn.fplay.list.RadioStationList.java

private void loadFavoritesInternal(Context context) throws IOException {
    FileInputStream fs = null;/*  www.  j av a  2  s. c om*/
    BufferedInputStream bs = null;
    try {
        fs = context.openFileInput("_RadioFav");
        bs = new BufferedInputStream(fs, 4096);
        final int version = Serializer.deserializeInt(bs);
        final int count = Math.min(Serializer.deserializeInt(bs), MAX_COUNT);
        if (version == 0x0100 && count > 0) {
            favorites.clear();
            for (int i = 0; i < count; i++)
                favorites.add(RadioStation.deserialize(bs, true));
        }
    } catch (IOException ex) {
        if (ex instanceof FileNotFoundException && fs == null) {
            favorites.clear();
        } else {
            throw ex;
        }
    } finally {
        try {
            if (bs != null)
                bs.close();
        } catch (Throwable ex) {
        }
        try {
            if (fs != null)
                fs.close();
        } catch (Throwable ex) {
        }
    }
}

From source file:org.montanafoodhub.base.get.CertificationHub.java

protected HashMap<String, Certification> readFromFile(Context context) {
    HashMap<String, Certification> myCertificationMap = new HashMap<String, Certification>();
    try {/*w w w  .  ja v a 2s  .com*/
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myCertificationMap, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of certifications loaded: " + myCertificationMap.size());
    return myCertificationMap;
}

From source file:org.montanafoodhub.base.get.BuyerHub.java

protected HashMap<String, Buyer> readFromFile(Context context) {
    HashMap<String, Buyer> myBuyerMap = new HashMap<String, Buyer>();
    try {//  w  w w  .  ja  va  2s.c o  m
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myBuyerMap, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of buyers loaded: " + myBuyerMap.size());
    return myBuyerMap;
}

From source file:org.dharmaseed.android.TeacherCursorAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {

    // Set teacher name
    clearView(view);/* ww w  .  jav a2s .c  o m*/
    TextView title = (TextView) view.findViewById(R.id.item_view_title);
    title.setText(cursor.getString(cursor.getColumnIndexOrThrow(DBManager.C.Teacher.NAME)).trim());

    // Get number of talks by this teacher
    TextView numTalks = (TextView) view.findViewById(R.id.item_view_detail1);
    numTalks.setText(cursor.getString(cursor.getColumnIndexOrThrow("talk_count")) + " talks");

    // Set teacher photo
    String photoFilename = DBManager
            .getTeacherPhotoFilename(cursor.getInt(cursor.getColumnIndexOrThrow(DBManager.C.Teacher.ID)));
    ImageView photoView = (ImageView) view.findViewById(R.id.item_view_photo);
    try {
        FileInputStream photo = context.openFileInput(photoFilename);
        photoView.setImageBitmap(BitmapFactory.decodeStream(photo));
    } catch (FileNotFoundException e) {
        Drawable icon = ContextCompat.getDrawable(context, R.drawable.dharmaseed_icon);
        photoView.setImageDrawable(icon);
    }

    // Set stars
    handleStars(view, cursor.getInt(cursor.getColumnIndexOrThrow(DBManager.C.Teacher.ID)));

}

From source file:org.montanafoodhub.base.get.ProducerHub.java

protected HashMap<String, Producer> readFromFile(Context context) {
    HashMap<String, Producer> myProducerMap = new HashMap<String, Producer>();
    try {//  w w  w  .jav  a2  s  .c  o m
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myProducerMap, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of producers loaded: " + myProducerMap.size());
    return myProducerMap;
}