June | 2014 | Techbirds

Posted on: June 30, 2014 /

Categories: Mobile Applications

Code to extract the images from canvas and save it into Sd card of mobile in Android :

//canvasview is an object or reference to Canvas

{ canvasview.setDrawingCacheEnabled(true); canvasview.buildDrawingCache(true); // MainActivity.bitmap2=canvasview.getDrawingCache(true); Bitmap imgData = Bitmap.createBitmap(canvasview

.getDrawingCache(true));

DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”);
Calendar cal = Calendar.getInstance();

storeImage(imgData, “/” + cal.getTime().toString().replace(‘:’, ‘_’) + “.png”); Toast.makeText(EditorActivity.this, “image saved to sd card”, Toast.LENGTH_SHORT).show();

}

private boolean storeImage(Bitmap imageData, String filename) {
// get path to external storage (SD card)

String iconsStoragePath = Environment.getExternalStorageDirectory() + “/facechanger/myImages/”;

File sdIconStorageDir = new File(iconsStoragePath);

// create storage directories, if they don’t exist
sdIconStorageDir.mkdirs();

try { String filePath = sdIconStorageDir.toString() + filename;

FileOutputStream fileOutputStream = new FileOutputStream(filePath);

BufferedOutputStream bos = new BufferedOutputStream(
fileOutputStream);

// choose another format if PNG doesn’t suit you
imageData.compress(CompressFormat.PNG, 100, bos);

bos.flush();
bos.close();

} catch (FileNotFoundException e) { Log.w(“TAG”, “Error saving image file: ” + e.getMessage()); return false; } catch (IOException e) { Log.w(“TAG”, “Error saving image file: ” + e.getMessage()); return false;

}

return true;
}

79 total views, 3 views today