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:Main.java

public static String imgCacheRead(Context context, String cacheImgFileName) {
    int len = 1024;
    byte[] buffer = new byte[len];
    try {//from w  ww .  java 2s. c om
        FileInputStream fis = context.openFileInput(cacheImgFileName);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int nrb = fis.read(buffer, 0, len); // read up to len bytes
        while (nrb != -1) {
            baos.write(buffer, 0, nrb);
            nrb = fis.read(buffer, 0, len);
        }
        buffer = baos.toByteArray();
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        return new String(buffer, "utf-8");
    } catch (UnsupportedEncodingException e) {

        return null;
    }
}

From source file:Main.java

public static Object readObject(Context context, String filename) {
    FileInputStream in = null;/*w w  w .  ja va 2  s .com*/
    ObjectInputStream oin = null;
    Object data = null;
    try {
        in = context.openFileInput(filename + ".odb");
        oin = new ObjectInputStream(in);
        data = oin.readObject();
        oin.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return data;
}

From source file:Main.java

public static String readInternalFileContent(Context content, String fileName) {
    String file = new String(), tmp;
    try {//from   w ww . j ava2 s . c  o  m
        // Read the file
        BufferedReader bf = new BufferedReader(new InputStreamReader(content.openFileInput(fileName)));
        while ((tmp = bf.readLine()) != null) {
            file += tmp;
        }
        bf.close();
    } catch (IOException e) {
        Log.e("JSO reading", "Error reading the file " + fileName + "\n" + e.getMessage());
    }
    return file;
}

From source file:nl.spellenclubeindhoven.dominionshuffle.DataReader.java

public static String readStringFromFile(Context context, String filename) {
    try {//from ww w .j  ava  2s . c o m
        return readStringFromStream(context.openFileInput(filename));
    } catch (FileNotFoundException ignore) {
        ignore.printStackTrace();
    }

    return "";
}

From source file:Main.java

public static int loadUserInfo(Context c) throws Exception {
    ArrayList<String> fileContents = new ArrayList<String>();
    BufferedReader br;/* w w w  .ja va 2 s.  c  o m*/

    br = new BufferedReader(new InputStreamReader(c.openFileInput(userInfoPath)));

    String nextLine = br.readLine();
    while (nextLine != null) {
        fileContents.add(nextLine);
        nextLine = br.readLine();
    }
    br.close();

    String[] info = new String[nFields];
    for (int i = 0; i < nFields; i++) {
        info[i] = "";
    }

    int nC = 0;
    for (String f : fileContents) {
        if (!f.equals(separator)) {
            info[nC] += f + eol;
        } else {
            info[nC] = info[nC].trim();
            nC += 1;
        }
    }
    userInfo = info;
    return nC;
}

From source file:Main.java

public static int[] fileToIntArray(Context myContext, String filename, int size) {

    int[] array = new int[size];
    int i = 0;/*from   ww  w.j  a  v a  2 s  .  c o  m*/
    FileInputStream inputStream;
    try {
        int c;
        inputStream = myContext.openFileInput(filename);
        StringWriter writer = new StringWriter();
        while ((c = inputStream.read()) != -1) {
            writer.append((char) c);
        }
        String ints[] = writer.toString().split("\n");
        for (String s : ints) {
            array[i++] = Integer.parseInt(s);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return array;
}

From source file:Main.java

private static boolean isOptedOut(Context appContext, boolean shouldAsk) {
    boolean optedOut = false;
    FileInputStream in = null;/*from  w  w w.  j a  v  a2 s  .c o m*/
    try {
        in = appContext.openFileInput(QCMEASUREMENT_OPTOUT_STRING);
        optedOut = in.read() != 0;
    } catch (FileNotFoundException e) {
        if (shouldAsk)
            askEveryone(appContext, false, false);
    } catch (IOException ignored) {
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ignored) {
        }
    }

    return optedOut;

}

From source file:io.lqd.sdk.model.LQLiquidPackage.java

public static LQLiquidPackage loadFromDisk(Context context) {
    LQLog.data("Loading from local storage");
    try {// ww w  . j a v  a  2  s  .  c o m
        FileInputStream fileInputStream = context.openFileInput(LIQUID_PACKAGE_FILENAME + ".vars");
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        Object result = objectInputStream.readObject();
        objectInputStream.close();
        return (LQLiquidPackage) result;
    } catch (IOException e) {
        LQLog.infoVerbose("Could not load liquid package from file");
    } catch (ClassNotFoundException e) {
        LQLog.infoVerbose("Could not load liquid package from file");
    }
    return new LQLiquidPackage();
}

From source file:it.nicola_amatucci.util.JsonAndroidLocalIO.java

public static <T> T loadData(Context context, String filename, Class<T> obj) {
    StringBuilder strContent = new StringBuilder("");
    try {/* w w  w  . j a  v a  2 s  .  c  o m*/
        BufferedInputStream in = new BufferedInputStream(context.openFileInput(filename));

        int ch;
        while ((ch = in.read()) != -1)
            strContent.append((char) ch);

        in.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (strContent.length() > 0) {
        try {
            Log.i("TAG", strContent.toString());
            JSONObject json = new JSONObject(strContent.toString());
            return Json.object_from_json(json, obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return null;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Parcelable readParcelable(Context context, String fileName, ClassLoader classLoader) {
    Parcelable parcelable = null;//  w  ww .  ja  v a2s.  c  o m
    FileInputStream fis = null;
    ByteArrayOutputStream bos = null;
    try {
        fis = context.openFileInput(fileName);
        if (fis != null) {
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[4096];
            int bytesRead;
            while ((bytesRead = fis.read(b)) != -1) {
                bos.write(b, 0, bytesRead);
            }

            byte[] data = bos.toByteArray();

            Parcel parcel = Parcel.obtain();
            parcel.unmarshall(data, 0, data.length);
            parcel.setDataPosition(0);
            parcelable = parcel.readParcelable(classLoader);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        parcelable = null;
    } finally {
        if (fis != null)
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        if (bos != null)
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    return parcelable;
}