Guest User

Untitled

a guest
Dec 18th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. File file = saveBitMap(MainActivity.this, mRootLayout);
  2. if (file != null) {
  3. Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
  4. materialDesignFAM.setVisibility(View.VISIBLE);
  5.  
  6. Log.i("TAG", "Drawing saved to the gallery!");
  7. } else {
  8. Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
  9. materialDesignFAM.setVisibility(View.VISIBLE);
  10. Log.i("TAG", "Oops! Image could not be saved.");
  11. }
  12. }
  13.  
  14. private File saveBitMap(Context context, View drawView) {
  15. File pictureFileDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "ShotOnLogo");
  16. if (!pictureFileDir.exists()) {
  17. boolean isDirectoryCreated = pictureFileDir.mkdirs();
  18. if (!isDirectoryCreated)
  19. Log.i("TAG", "Can't create directory to save the image");
  20. return null;
  21. }
  22. String filename = pictureFileDir.getPath() + File.separator + System.currentTimeMillis() + ".jpg";
  23. File pictureFile = new File(filename);
  24. Bitmap bitmap = getBitmapFromView(drawView);
  25. try {
  26. pictureFile.createNewFile();
  27. FileOutputStream oStream = new FileOutputStream(pictureFile);
  28. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, oStream);
  29.  
  30. oStream.flush();
  31.  
  32. Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
  33.  
  34. oStream.close();
  35.  
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. Log.i("TAG", "There was an issue saving the image.");
  39. }
  40. scanGallery(context, pictureFile.getAbsolutePath());
  41. return pictureFile;
  42. }
  43.  
  44. //create bitmap from view and returns it
  45. //create bitmap from view and returns it
  46. private Bitmap getBitmapFromView(View view) {
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. //Define a bitmap with the same size as the view
  56. Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
  57. //Bind a canvas to it
  58. Canvas canvas = new Canvas(returnedBitmap);
  59.  
  60.  
  61.  
  62.  
  63. //Get the view's background
  64. Drawable bgDrawable = view.getBackground();
  65.  
  66. if (bgDrawable != null) {
  67. //has background drawable, then draw it on the canvas
  68. bgDrawable.draw(canvas);
  69. } else {
  70. //does not have background drawable, then draw white background on the canvas
  71. canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
  72. }
  73. // draw the view on the canvas
  74. view.draw(canvas);
  75. //return the bitmap
  76. return returnedBitmap;
  77. }
Add Comment
Please, Sign In to add comment