Android Bitmap Save saveBitmap2SD(Bitmap bitmap, Context context, String filePath, String fileName)

Here you can find the source of saveBitmap2SD(Bitmap bitmap, Context context, String filePath, String fileName)

Description

save Bitmap SD

Declaration

public static boolean saveBitmap2SD(Bitmap bitmap, Context context,
            String filePath, String fileName) 

Method Source Code

//package com.java2s;
import android.content.Context;
import android.content.Intent;

import android.graphics.Bitmap;

import android.net.Uri;
import android.os.Environment;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static boolean saveBitmap2SD(Bitmap bitmap, Context context,
            String filePath, String fileName) {
        String sdStatus = Environment.getExternalStorageState();
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { //Check sd whether usable.
            return false;
        }// ww  w  .  j  av  a 2s  . c o m
        String name = new SimpleDateFormat().format(new Date()) + ".jpg";
        String filePath1 = "/sdcard/DCIM/Camera/";
        File file = new File(filePath1);
        if (!file.exists()) {
            if (!file.mkdirs())
                return false;
        }
        String fileName1 = filePath1 + name;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(fileName1);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            bos.flush();
            bos.close();
            fos.flush();
            //fos.write(data);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //        MediaStore.Images.Media.insertImage(context.getContentResolver(),bitmap,"","");
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
                .parse("file://"
                        + Environment.getExternalStorageDirectory())));
        return true;
    }
}

Related

  1. saveImage(String filename, Bitmap bm, boolean landscape)
  2. saveMyBitmap(String bitName, Bitmap mBitmap)
  3. saveToLocal(Bitmap bmp, String fileName)
  4. storeImage(Context context, Bitmap image)
  5. saveBitmap(Bitmap bitmap, String filename)
  6. addImageAsApplication(ContentResolver cr, String name, long dateTaken, String directory, String filename, Bitmap source, byte[] jpegData)
  7. saveBitmapToFile(Bitmap bitmap, String path)
  8. save(Bitmap bitmap, String fileName)
  9. saveBitmapToFile(Bitmap bitmap, String filename)