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 void setCookie(Context context, String url) {
    FileInputStream in = null;//from w  w  w. j  a v  a2 s.c om
    try {
        in = context.openFileInput(TAXICOOKIE_FILE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    if (in == null) {
        Log.w(TAG, "saveCookie: Cannot open file: " + TAXICOOKIE_FILE);
    }

    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String cookieStr = null;
    try {
        cookieStr = reader.readLine();
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.d(TAG, "cookieStr: " + cookieStr);
    if (cookieStr == null) {
        return;
    }

    CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeSessionCookie();
    cookieManager.setCookie(url, cookieStr);
    CookieSyncManager.getInstance().sync();
}

From source file:Main.java

/**
 * (android) read from file// w  w w .j ava2 s  .c om
 * 
 * @param fileName
 * @return
 */
public static String androidFileload(Context con, String fileName) {
    Properties properties = new Properties();
    try {
        FileInputStream stream = con.openFileInput(fileName);
        properties.load(stream);
    } catch (FileNotFoundException e) {
        return null;
    } catch (IOException e) {
        return null;
    }

    return properties.get(FILE_ENCODING).toString();
}

From source file:Main.java

public static Bitmap getBitmap(Context context, String fileName) {
    FileInputStream fis = null;//from   ww w.  ja  v a  2  s  .  c  o m
    Bitmap bitmap = null;
    try {
        fis = context.openFileInput(fileName);
        bitmap = BitmapFactory.decodeStream(fis);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    } finally {
        try {
            fis.close();
        } catch (Exception e) {
        }
    }
    return bitmap;
}

From source file:Main.java

public static InputStream readStreamFromFile(String fileName, Context context) {

    FileInputStream fis = null;/* w w  w  .  ja v a 2 s . c  o  m*/
    try {
        // First read the user agent from private memory
        fis = context.openFileInput(fileName);// listType as filename

    } catch (Exception exp) {
        Log.v(TAG, "GetMedia list lastupdated time - exp:" + exp.getMessage());
    }
    return fis;
}

From source file:Main.java

public static Bitmap getBitmap(Context context, String fileName) {
    FileInputStream fis = null;//from   w ww .j  ava2 s  .c  o  m
    Bitmap bitmap = null;
    try {
        fis = context.openFileInput(fileName);
        bitmap = BitmapFactory.decodeStream(fis);
    } catch (FileNotFoundException e) {
    } catch (OutOfMemoryError e) {
    } finally {
        try {
            fis.close();
        } catch (Exception e) {
        }
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap getBitmap(Context context, String fileName) {
    FileInputStream fis = null;//from   www  . j a  v a2 s.  c o  m
    Bitmap bitmap = null;
    try {
        fis = context.openFileInput(fileName);
        bitmap = BitmapFactory.decodeStream(fis);
    } catch (FileNotFoundException | OutOfMemoryError e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}

From source file:Main.java

public static byte[] readByteArray(Context context, String fileName) throws IOException {

    byte[] data;/*from   w w  w  .  ja v  a  2 s  .c o m*/
    int c;

    FileInputStream fis = context.openFileInput(fileName);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BufferedInputStream bos = new BufferedInputStream(fis);

    while ((c = bos.read()) != -1) {
        byteArrayOutputStream.write(c);
    }

    data = byteArrayOutputStream.toByteArray();

    bos.close();
    byteArrayOutputStream.close();
    fis.close();

    return data;
}

From source file:Main.java

public static Bitmap readBitmapFromFile(Context context, String imageName) {
    Bitmap bitmap = null;/*w w w .  j  a  v  a  2  s  . c o m*/
    try {
        //read avatar file
        FileInputStream fileInputStream = context.openFileInput(imageName);
        bitmap = BitmapFactory.decodeStream(fileInputStream);
        fileInputStream.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:com.kyon.klib.base.KFileUtils.java

public static String readFile(Context context, String fileName) throws IOException {
    String res = "";
    try {/*from   ww  w. j a va2 s .  c o m*/
        FileInputStream fIn = context.openFileInput(fileName);
        int length = fIn.available();
        byte[] buffer = new byte[length];
        fIn.read(buffer);
        res = EncodingUtils.getString(buffer, "UTF-8");
        fIn.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return res;

}

From source file:Main.java

public static Bitmap loadScaledBitmap(Context context, String bitmapFilePath, int widthDp, int heightDp)
        throws IOException {
    //create movie icon
    Bitmap bitmap;//ww  w  .  java2  s.c  o  m
    bitmap = BitmapFactory.decodeStream(context.openFileInput(bitmapFilePath));
    bitmap = Bitmap.createScaledBitmap(bitmap, widthDp, heightDp, true);
    return bitmap;
}