Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. import android.content.Context;
  2. import android.content.ContextWrapper;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5.  
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import static openaccount.laxmi.app.log.Logger.LOGD;
  13. import static openaccount.laxmi.app.log.Logger.makeLogTag;
  14.  
  15. /**
  16. * Created by mubin on 4/24/16.
  17. */
  18. public class ImageSaver {
  19. private static final String TAG = makeLogTag(ImageSaver.class);
  20.  
  21. /**
  22. * Methos to save the bitmap to the internal memory
  23. * @param image : bitmap image to save
  24. * @param accountNumber : account number
  25. * @param context
  26. * @param imageType : type of image. Please check constant.java
  27. * @return
  28. */
  29. public static String storeImage(Bitmap image, String accountNumber, Context context, Integer imageType) {
  30. ContextWrapper cw = new ContextWrapper(context);
  31. // path to /data/data/yourapp/app_data/imageDir
  32. // if folder is not present it will be created automatically
  33. File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
  34.  
  35. String fileName = getFileName(accountNumber, imageType);
  36.  
  37. File mypath=new File(directory,fileName);
  38.  
  39. FileOutputStream fos = null;
  40. try {
  41. fos = new FileOutputStream(mypath);
  42. // Use the compress method on the BitMap object to write image to the OutputStream
  43. image.compress(Bitmap.CompressFormat.PNG, 100, fos);
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. } finally {
  47. try {
  48. fos.close();
  49. } catch (IOException e) {
  50. LOGD(TAG, "Cannot close the stream");
  51. }
  52. }
  53. return directory.getAbsolutePath();
  54. }
  55.  
  56. private static String getPrefixFileName(Integer imageType) {
  57. switch (imageType){
  58. case 0:
  59. return "right_thumb";
  60. case 1:
  61. return "left_thumb";
  62. case 2:
  63. return "citizenship";
  64. case 3:
  65. return "passport";
  66. case 4:
  67. return "license";
  68. case 5:
  69. return "profile";
  70. case 6:
  71. return "signature";
  72. default:
  73. return null;
  74. }
  75. }
  76.  
  77. /**
  78. * Every account Number will have at most 7 images.
  79. * Each and every file should be unique.
  80. *
  81. *
  82. * @param accountNumber
  83. * @param imageType
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement