Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- File file = saveBitMap(MainActivity.this, mRootLayout);
- if (file != null) {
- Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
- materialDesignFAM.setVisibility(View.VISIBLE);
- Log.i("TAG", "Drawing saved to the gallery!");
- } else {
- Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
- materialDesignFAM.setVisibility(View.VISIBLE);
- Log.i("TAG", "Oops! Image could not be saved.");
- }
- }
- private File saveBitMap(Context context, View drawView) {
- File pictureFileDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "ShotOnLogo");
- if (!pictureFileDir.exists()) {
- boolean isDirectoryCreated = pictureFileDir.mkdirs();
- if (!isDirectoryCreated)
- Log.i("TAG", "Can't create directory to save the image");
- return null;
- }
- String filename = pictureFileDir.getPath() + File.separator + System.currentTimeMillis() + ".jpg";
- File pictureFile = new File(filename);
- Bitmap bitmap = getBitmapFromView(drawView);
- try {
- pictureFile.createNewFile();
- FileOutputStream oStream = new FileOutputStream(pictureFile);
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, oStream);
- oStream.flush();
- Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
- oStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- Log.i("TAG", "There was an issue saving the image.");
- }
- scanGallery(context, pictureFile.getAbsolutePath());
- return pictureFile;
- }
- //create bitmap from view and returns it
- //create bitmap from view and returns it
- private Bitmap getBitmapFromView(View view) {
- //Define a bitmap with the same size as the view
- Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
- //Bind a canvas to it
- Canvas canvas = new Canvas(returnedBitmap);
- //Get the view's background
- Drawable bgDrawable = view.getBackground();
- if (bgDrawable != null) {
- //has background drawable, then draw it on the canvas
- bgDrawable.draw(canvas);
- } else {
- //does not have background drawable, then draw white background on the canvas
- canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
- }
- // draw the view on the canvas
- view.draw(canvas);
- //return the bitmap
- return returnedBitmap;
- }
Add Comment
Please, Sign In to add comment