Guest User

Untitled

a guest
Feb 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. view.setDrawingCacheEnabled(true);
  2. Bitmap bitmap = view.getDrawingCache();
  3. try {
  4. FileOutputStream stream = new FileOutputStream(getApplicationContext().getCacheDir() + "/image.jpg");
  5. bitmap.compress(CompressFormat.JPEG, 80, stream);
  6. stream.close();
  7. } catch (FileNotFoundException e) {
  8. e.printStackTrace();
  9. } catch (IOException e) {
  10. e.printStackTrace();
  11. }
  12. view.setDrawingCacheEnabled(false);
  13.  
  14. final boolean cachePreviousState = view.isDrawingCacheEnabled();
  15. final int backgroundPreviousColor = view.getDrawingCacheBackgroundColor();
  16. view.setDrawingCacheEnabled(true);
  17. view.setDrawingCacheBackgroundColor(0xfffafafa);
  18. final Bitmap bitmap = view.getDrawingCache();
  19. view.setDrawingCacheBackgroundColor(backgroundPreviousColor);
  20. bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
  21. view.setDrawingCacheEnabled(cachePreviousState);
  22.  
  23. public Bitmap loadBitmapFromView(View v) {
  24. DisplayMetrics dm = getResources().getDisplayMetrics();
  25. v.measure(View.MeasureSpec.makeMeasureSpec(dm.widthPixels,
  26. View.MeasureSpec.EXACTLY),
  27. View.MeasureSpec.makeMeasureSpec(dm.heightPixels,
  28. View.MeasureSpec.EXACTLY));
  29. v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
  30. Bitmap returnedBitmap =
  31. Bitmap.createBitmap(v.getMeasuredWidth(),
  32. v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
  33. Canvas c = new Canvas(returnedBitmap);
  34. v.draw(c);
  35.  
  36. return returnedBitmap;
  37. }
Add Comment
Please, Sign In to add comment