Example usage for java.io FileInputStream close

List of usage examples for java.io FileInputStream close

Introduction

In this page you can find the example usage for java.io FileInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this file input stream and releases any system resources associated with the stream.

Usage

From source file:Main.java

public static String getStringFromFile(String filePath) throws Exception {
    File fl = new File(filePath);
    FileInputStream fin = new FileInputStream(fl);
    String ret = convertStreamToString(fin);
    // Make sure you close all streams.
    fin.close();
    return ret;//from w  w w . java2s  .c om
}

From source file:Main.java

/**
 * <pre>/*from  www .ja  va 2 s.  co  m*/
 * Read binary data from file stored in app 's internal memory.
 * </pre>
 * @param absolutePath absolute path to source file
 * @return binary data
 */
public static byte[] readInternalFile(String absolutePath) throws IOException {
    FileInputStream in = getCurrentContext().openFileInput(absolutePath);
    byte[] b = new byte[in.available()];
    in.read(b);
    in.close();

    return b;
}

From source file:Main.java

/**
 * <pre>//from w  w  w  .j  a  v a  2  s  . c o m
 * Get width and height of bitmap from file.
 * </pre>
 * @param path path to file
 * @return integer array with 2 elements: width and height
 */
public static int[] getBitmapSizes(String path) throws IOException {
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    FileInputStream is = new FileInputStream(path);
    BitmapFactory.decodeStream(is, null, bmOptions);
    is.close();
    return new int[] { bmOptions.outWidth, bmOptions.outHeight };
}

From source file:Main.java

public static Bitmap readBitmapFromFile(Context context, String imageName) {
    Bitmap bitmap = null;// ww  w  . ja  v  a2 s  .c  om
    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:Main.java

/**
 * decode the image bitmap according to specified size
 * //from  w w w  . j a v  a2  s . c o  m
 * @param f
 * @param maxSize
 * @return
 */
public static Bitmap decodeFile(File f, int maxSize) {
    Bitmap b = null;
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStream fis = new FileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

        int scale = 1;
        if (o.outHeight > maxSize || o.outWidth > maxSize) {
            scale = (int) Math.pow(2, (int) Math
                    .round(Math.log(maxSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        fis = new FileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (IOException e) {
    }
    return b;
}

From source file:Main.java

public static Bitmap readBitmapFile(String aFileName, Options aOptions) {
    Bitmap bitmap = null;/*ww w  .ja  v  a2s .c  o m*/
    File file = new File(aFileName);
    try {
        FileInputStream fis = new FileInputStream(file);
        bitmap = BitmapFactory.decodeStream(fis, null, aOptions);
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

/**
 * <pre>/*from   w ww  .ja  va  2 s . co m*/
 * Read binary data from arbitrary file.
 * </pre>
 * @param absolutePath absolute path to source file
 * @return binary data
 */
public static byte[] readFile(String absolutePath) throws IOException {
    File file = new File(absolutePath);
    if (!file.exists()) {
        return null;
    }

    FileInputStream in = new FileInputStream(file);
    byte[] b = new byte[in.available()];
    in.read(b);
    in.close();

    return b;
}

From source file:Main.java

public static Bitmap getSimilarBitmap(File f, int width, int height) {
    try {//from www  . j a  v  a 2  s .  c  o  m
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        FileInputStream stream1 = new FileInputStream(f);
        BitmapFactory.decodeStream(stream1, null, o);
        stream1.close();

        float widthScale = o.outWidth / width;
        float heightScale = o.outHeight / height;
        float scale = widthScale > heightScale ? widthScale : heightScale;
        if (scale > 8) {
            o.inSampleSize = 8;
        } else if (scale > 6) {
            o.inSampleSize = 6;
        } else if (scale > 4) {
            o.inSampleSize = 4;
        } else if (scale > 2) {
            o.inSampleSize = 2;
        } else {
            o.inSampleSize = 1;
        }
        o.inJustDecodeBounds = false;
        FileInputStream stream2 = new FileInputStream(f);
        Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o);
        stream2.close();
        return bitmap;
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

public static Bitmap decodeFile(File theFile, int IMAGE_MAX_SIZE) {
    Bitmap bmp = null;/*from   w w  w  . ja  va 2s .c  o  m*/
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStream fis = new FileInputStream(theFile);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

        int scale = 1;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = (int) Math.pow(2, (int) Math.round(
                    Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        fis = new FileInputStream(theFile);
        bmp = BitmapFactory.decodeStream(fis, null, o2);

        fis.close();
    } catch (IOException e) {
    }
    return bmp;
}

From source file:Main.java

public static String encodeToFile(File file) {
    try {/*  ww  w . j av  a2  s  .  c om*/
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int) file.length()];
        inputFile.read(buffer);
        inputFile.close();
        return Base64.encodeToString(buffer, Base64.NO_WRAP);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}