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:net.giovannicapuano.visualnovel.Memory.java

public static String getLastScript(Context context) {
    StringBuffer buffer = new StringBuffer();

    try {/*  www . ja  v  a2s. c  om*/
        InputStreamReader input = new InputStreamReader(context.openFileInput(SCRIPT));
        BufferedReader reader = new BufferedReader(input);
        String line;
        while ((line = reader.readLine()) != null)
            buffer.append(line);
    } catch (Exception e) {
        Utils.error(e);
    }

    return buffer.toString();
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static List<Parcelable> readParcelableList(Context context, String fileName, ClassLoader classLoader) {
    List<Parcelable> results = null;
    FileInputStream fis = null;//from  ww w  . j  a va2  s.  co  m
    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);
            results = parcel.readArrayList(classLoader);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        results = 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 results;
}

From source file:Main.java

public static List<String> readFromFile(String file, Context context) {
    List<String> readLines = new ArrayList<String>();
    try {/*w  w w  . j  a  v  a  2s.  c om*/
        // File readFile = new File(config.getApp().getDir("files",
        // Context.MODE_WORLD_READABLE), file);

        BufferedReader reader = toReader(context.openFileInput(file));
        String read = null;
        while ((read = reader.readLine()) != null) {
            readLines.add(read);
        }
        reader.close();

    } catch (IOException e) {

    }
    return readLines;
}

From source file:Main.java

public static Bitmap getBitmap(Context context, String imageFile) {
    try {//from   w w w. java2s . c o m
        if (imageFile != null) {
            if (imageFile.contains("/")) {
                imageFile = imageFile.substring(imageFile.lastIndexOf("/") + 1);
            }
            FileInputStream in = context.openFileInput(imageFile);
            return BitmapFactory.decodeStream(in);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String readFile(Context context, String fileName) {
    if (!exists(context, fileName)) {
        return null;
    }/*from   ww w.  j  a  va2  s .  c  om*/
    FileInputStream fis = null;
    String content = null;
    try {
        fis = context.openFileInput(fileName);
        if (fis != null) {

            byte[] buffer = new byte[1024];
            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            while (true) {
                int readLength = fis.read(buffer);
                if (readLength == -1)
                    break;
                arrayOutputStream.write(buffer, 0, readLength);
            }
            fis.close();
            arrayOutputStream.close();
            content = new String(arrayOutputStream.toByteArray());

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        content = null;
    } finally {
        try {
            if (fis != null)
                fis.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return content;
}

From source file:ca.tbcn.greenp.GreenParkingApp.java

/**
 * Load json from stored file, create file if it's not there
 * //from w w  w  . j a  va 2 s . com
 * @param context
 * @return
 * @throws IOException
 */
public static String getJsonFileContents(Context context) throws IOException {
    if (!Util.fileExists(context, JSON_FILE_NAME)) {
        seedJsonFile(context);
    }

    FileInputStream fis = context.openFileInput(JSON_FILE_NAME);

    String contents = Util.inputStreamToString(fis);
    fis.close();

    return contents;
}

From source file:net.giovannicapuano.visualnovel.Memory.java

public static Hashtable<String, String> getKeyValues(Context context) {
    Hashtable<String, String> keyvalues = new Hashtable<String, String>();

    try {//ww  w.  ja  v  a  2  s. co  m
        StringBuffer buffer = new StringBuffer();

        InputStreamReader input = new InputStreamReader(context.openFileInput(KEYVALUES));
        BufferedReader reader = new BufferedReader(input);

        String line;
        while ((line = reader.readLine()) != null)
            buffer.append(line);

        JSONObject json = new JSONObject(buffer.toString());
        Iterator<?> keys = json.keys();

        while (keys.hasNext()) {
            String key = (String) keys.next();
            keyvalues.put(key, json.getString(key));
        }
    } catch (Exception e) {
        Utils.error(e);
    }

    return keyvalues;
}

From source file:org.wahtod.wififixer.utility.LogUtil.java

private static String getStackTrace(Context context) {
    StringBuilder trace = new StringBuilder();
    DataInputStream d;// ww  w . j  av a 2 s .co m
    try {
        d = new DataInputStream(context.openFileInput(DefaultExceptionHandler.EXCEPTIONS_FILENAME));
    } catch (FileNotFoundException e1) {
        return trace.append(context.getString(R.string.no_stack_trace_found)).toString();
    }
    for (;;) {
        try {
            trace.append(d.readUTF());
        } catch (EOFException e) {
            trace.append(e);
        } catch (IOException e) {
            trace.append(e);
        } finally {
            try {
                d.close();
            } catch (IOException e) {
                trace.append(e);
            }
        }
        context.deleteFile(DefaultExceptionHandler.EXCEPTIONS_FILENAME);
        return trace.toString();
    }
}

From source file:ru.kaefik.isaifutdinov.an_weather_widget.utils.Utils.java

public static String openFile(String filename, Context context) {
    String res = "";
    try {/*from ww  w. j  a v a 2 s  . co  m*/
        FileInputStream inputStream = context.openFileInput(filename);
        if (inputStream != null) {
            InputStreamReader isr = new InputStreamReader(inputStream);
            BufferedReader reader = new BufferedReader(isr);
            String line;
            StringBuilder builder = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            inputStream.close();
            res = builder.toString();
        }
    } catch (Throwable t) {
        // TODO: ?  ??   ,  ?  ?  
    }
    return res;
}

From source file:com.jamesgiang.aussnowcam.Utils.java

public static String ReadSettings(Context context, String file) throws IOException {
    FileInputStream fIn = null;/* w ww . j  a  v a  2s  .com*/
    InputStreamReader isr = null;
    String data = null;
    fIn = context.openFileInput(file);
    isr = new InputStreamReader(fIn);
    char[] inputBuffer = new char[fIn.available()];
    isr.read(inputBuffer);
    data = new String(inputBuffer);
    isr.close();
    fIn.close();
    return data;
}