Guest User

Untitled

a guest
Aug 20th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. package com.example.bogna.hogmessanger;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.os.AsyncTask;
  6. import android.util.Log;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.OutputStream;
  13.  
  14. /**
  15. * Created by Janek on 2016-08-19.
  16. */
  17. public class PutBitmap extends AsyncTask<Void,Void,Boolean> {
  18. File cacheDir;
  19. String name;
  20. Bitmap img;
  21.  
  22. public PutBitmap(Context context, String src, Bitmap bitmap){
  23. cacheDir = context.getCacheDir();
  24. name = src;
  25. img = bitmap;
  26. if(!cacheDir.mkdirs()) {
  27. cacheDir.mkdirs();
  28. }
  29. }
  30.  
  31. @Override
  32. protected Boolean doInBackground(Void... params) {
  33. Boolean isOk = true;
  34. Log.d("hejooo","cache");
  35. Log.d("cacheDir",cacheDir.toString());
  36. OutputStream fOut = null;
  37. File file = new File(cacheDir,name+".jpg");
  38. try {
  39. fOut = new FileOutputStream(file);
  40. } catch (FileNotFoundException e) {
  41. e.printStackTrace();
  42. Log.d("niestety1",e.getMessage());
  43.  
  44. }
  45. if(!img.compress(Bitmap.CompressFormat.PNG, 100, fOut)){
  46. isOk = false;
  47. }
  48. try {
  49. fOut.flush();
  50. fOut.close();
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. Log.d("niestety",e.getMessage());
  54. }
  55. return isOk;
  56. }
  57. }
Add Comment
Please, Sign In to add comment