Android:save a bitmap to location with particular resolution

  • FileOutputStream
1. try {
       FileOutputStream out = new FileOutputStream(filename);
       bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
       out.flush();
       out.close();
} catch (Exception e) {
       e.printStackTrace();
}

2.File f = File(context.getCacheDir(), filename);
f.createNewFile();
//Convert bitmap to byte array
Bitmap bitmap = your bitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);

The compress method of Bitmap to create an OutputStream which can be written to the SD card. But, compress() can’t change the size of the image, only the format and (potentially) quality (no effect when saving as PNG which is lossless; for jpeg, it works 0-100)

  • BitmapFactory

BitmapFactory can convert a File to a Bitmap

    • BitmapFactory.option

 public static File resizePhoto(Context context, String filePath, String resizethrows IOException {
        BitmapFactory.Options option;
        Bitmap originalBitmap, resizedBitmap;
        
        File cacheDirectory = getCacheDirectory(context, "images");
        if (!cacheDirectory.exists()) {
            cacheDirectory.mkdirs();
            new File(cacheDirectory, ".nomedia").createNewFile();
        }
        
        File newFilePath = new File(cacheDirectory, System.currentTimeMillis() ".jpg");
        
        option = new BitmapFactory.Options();
        option.inJustDecodeBounds = true;
        originalBitmap = BitmapFactory.decodeFile(filePath, option);
        int w = option.outWidth, h = option.outHeight;
        int rw, rh;
        float newSize = "no".equals(resize) ? 0.0f : "medium".equals(resize) ? MEDIUM_SIZE : SMALL_SIZE;
        
        if (newSize == 0.0f) {
            FileInputStream fis = new FileInputStream(filePath);
            FileOutputStream fos = new FileOutputStream(newFilePath);
            copy(fis, fos);
            fis.close();
            fos.close();
            return newFilePath;
        else {
            if (w > h && w >= newSize) {
                rw = (intnewSize;
                rh = (int) (h * newSize / w);
            else if (h > w && h >= newSize) {
                rh = (intnewSize;
                rw = (int) (w * newSize / h);
            else {
                rh = h;
                rw = w;
            }
            
            int scale = 1;
            while (true) {
                if (w / < newSize || h / < newSize) {
                    break;
                }
                w /= 2;
                h /= 2;
                scale++;
            }
            option = new BitmapFactory.Options();
            option.inSampleSize = scale - 1;
            originalBitmap = BitmapFactory.decodeFile(filePath, option);
            resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, rw, rh, true);
            
            FileOutputStream fos = new FileOutputStream(newFilePath);
            resizedBitmap.compress(CompressFormat.JPEG, 100, fos);
            fos.close();
            
            return newFilePath;
        }
    }

  • MediaStore

How do I save data from Camera to disk using MediaStore on Android?

MediaStore.Images.Media.insertImage
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: