Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Saving created bitmap to directory on sd card

I created a bitmap and now i want to save that bitmap to a directory somewhere. Can anyone show me how this is done. Thanks

FileInputStream in;           BufferedInputStream buf;            try {                   in = new FileInputStream("/mnt/sdcard/dcim/Camera/2010-11-16_18-57-18_989.jpg");                   buf = new BufferedInputStream(in);                   Bitmap _bitmapPreScale = BitmapFactory.decodeStream(buf);                   int oldWidth = _bitmapPreScale.getWidth();                   int oldHeight = _bitmapPreScale.getHeight();                   int newWidth = 2592;                    int newHeight = 1936;                    float scaleWidth = ((float) newWidth) / oldWidth;                   float scaleHeight = ((float) newHeight) / oldHeight;                    Matrix matrix = new Matrix();                // resize the bit map                   matrix.postScale(scaleWidth, scaleHeight);                   Bitmap _bitmapScaled = Bitmap.createBitmap(_bitmapPreScale, 0, 0,  oldWidth, oldHeight, matrix, true); 

(I want to save _bitmapScaled to a folder on my SD card)

like image 929
user516883 Avatar asked Nov 24 '10 04:11

user516883


People also ask

How do I save pictures from internal storage on Android?

This example demonstrates How to write an image file in internal storage in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

Hi You can write data to bytes and then create a file in sdcard folder with whatever name and extension you want and then write the bytes to that file. This will save bitmap to sdcard.

ByteArrayOutputStream bytes = new ByteArrayOutputStream(); _bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes);  //you can create a new file name "test.jpg" in sdcard folder. File f = new File(Environment.getExternalStorageDirectory()                         + File.separator + "test.jpg"); f.createNewFile(); //write the bytes in file FileOutputStream fo = new FileOutputStream(f); fo.write(bytes.toByteArray());  // remember close de FileOutput fo.close(); 
like image 93
krunal shah Avatar answered Oct 02 '22 16:10

krunal shah