Example usage for java.io FileOutputStream close

List of usage examples for java.io FileOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

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

Usage

From source file:Main.java

public static Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;/*from  w  ww.  j a v  a  2s .c om*/
    if (drawable instanceof BitmapDrawable) {
        bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
        return null;
    }
    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}

From source file:Main.java

@SuppressWarnings("unused")
private static void saveImage(byte[] data) {
    File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
    if (pictureFile == null) {
        Log.d(TAG, "Error creating media file, check storage permissions");
        return;//from  w  w w  .  j ava  2s  .com
    }

    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        fos.write(data);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d(TAG, "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, "Error accessing file: " + e.getMessage());
    }
}

From source file:Main.java

public static boolean writeImage(File file, Bitmap mBitmap) {
    try {/* w  ww . j av  a  2 s.  com*/
        FileOutputStream fo = new FileOutputStream(file);
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fo);
        fo.flush();
        fo.close();
    } catch (Exception e) {
        System.out.println(e.getLocalizedMessage());
        return false;
    }
    return true;
}

From source file:Main.java

public static void saveBitmap(Bitmap bitmap, Bitmap.CompressFormat format, File target) {
    if (target.exists()) {
        target.delete();//from   ww w .  j  ava  2 s .  c  om
    }
    try {
        FileOutputStream out = new FileOutputStream(target);
        bitmap.compress(format, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void createFile(byte[] byteArray) {
    try {/*from   w  w  w  .  j a v a2s  . c  o m*/

        File file = new File("C:/Programming/compressedFile");

        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream("C://Programming/compressedFile");
        fos.write(byteArray, 0, byteArray.length);
        fos.close();

        System.out.println("Done");

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

}

From source file:Main.java

public static String saveToLocal(Bitmap bm) {
    String path = "/sdcard/test.jpg";
    try {//from w w w . j a  v  a  2s.  c  o  m
        FileOutputStream fos = new FileOutputStream(path);
        bm.compress(CompressFormat.JPEG, 75, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    return path;
}

From source file:de.huxhorn.lilith.tools.CreateMd5Command.java

public static boolean createMd5(File input) {
    final Logger logger = LoggerFactory.getLogger(CreateMd5Command.class);

    if (!input.isFile()) {
        if (logger.isWarnEnabled())
            logger.warn("{} isn't a file!", input.getAbsolutePath());
        return false;
    }//from w  ww  .j  a v  a2  s. c  o m
    File output = new File(input.getParentFile(), input.getName() + ".md5");

    try {

        FileInputStream fis = new FileInputStream(input);
        byte[] md5 = ApplicationPreferences.getMD5(fis);
        if (md5 == null) {
            if (logger.isWarnEnabled()) {
                logger.warn("Couldn't calculate checksum for {}!", input.getAbsolutePath());
            }
            return false;
        }
        FileOutputStream fos = new FileOutputStream(output);
        fos.write(md5);
        fos.close();
        if (logger.isInfoEnabled()) {
            logger.info("Wrote checksum of {} to {}.", input.getAbsolutePath(), output.getAbsolutePath());
            logger.info("MD5: {}", Hex.encodeHexString(md5));
        }
    } catch (IOException e) {
        if (logger.isWarnEnabled())
            logger.warn("Exception while creating checksum!", e);
        return false;
    }
    return true;
}

From source file:Main.java

public static void compressIt(String inputFile, String outputFile) {
    try {/* www .j  av  a 2s.  co  m*/
        Bitmap bmp = BitmapFactory.decodeFile(inputFile);
        FileOutputStream out = new FileOutputStream(new File(outputFile));
        bmp.compress(Bitmap.CompressFormat.PNG, 50, out); //100-best quality
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Writes the given value into the given file
 *
 * @return true on success, false on failure
 *//* w  w  w.j  a v  a2 s  . c  o  m*/
public static boolean writeLine(String fileName, String value) {
    try {
        FileOutputStream fos = new FileOutputStream(fileName);
        fos.write(value.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        Log.e(TAG, "Could not write to file " + fileName, e);
        return false;
    }

    return true;
}

From source file:Main.java

public static File downSample(Context context, Uri uri) throws Exception {
    Bitmap b = null;/*from   w  w w  .j av  a 2 s  . co m*/

    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;

    int scale = 1;
    if (o.outHeight > MAX_SIZE || o.outWidth > MAX_SIZE) {
        scale = (int) Math.pow(2, (int) Math
                .round(Math.log(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;
    InputStream is = context.getContentResolver().openInputStream(uri);
    b = BitmapFactory.decodeStream(is, null, o2);
    is.close();

    File outputDir = context.getCacheDir();
    File outputFile = File.createTempFile("avatar", ".jpg", outputDir);
    FileOutputStream fos = new FileOutputStream(outputFile);
    b.compress(Bitmap.CompressFormat.JPEG, 80, fos);
    fos.close();

    return outputFile;
}