Guest User

Untitled

a guest
May 27th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. public class ImageUtility {
  2.  
  3. public static void LoadImage(Context context, String url, ImageView imageView) {
  4. Picasso.with(context).load(url).into(imageView);
  5. }
  6.  
  7. public static Bitmap getImageFromBase64(String encodedImage) {
  8.  
  9. String base64Image = encodedImage.split(",")[1];
  10.  
  11. byte[] decodedString = Base64.decode(base64Image, Base64.DEFAULT);
  12. Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
  13.  
  14. return decodedByte;
  15. }
  16.  
  17. public static void saveImage(Bitmap finalBitmap, String image_name) {
  18.  
  19. String root = Environment.getExternalStorageDirectory().toString();
  20. File myDir = new File(root);
  21. myDir.mkdirs();
  22. String fname = "Image-" + image_name + ".jpg";
  23. File file = new File(myDir, fname);
  24. if (file.exists()) file.delete();
  25. Log.i("LOAD", root + fname);
  26. try {
  27. FileOutputStream out = new FileOutputStream(file);
  28. finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
  29. out.flush();
  30. out.close();
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. public static File createDirectoryAndSaveFile(Bitmap imageToSave, String name, Activity context) {
  37.  
  38. String fileName = name + ".jpg";
  39.  
  40. String string = context.getString(R.string.app_name);
  41. File direct = new File(Environment.getExternalStorageDirectory() + "/" + string);
  42.  
  43. if (!direct.exists()) {
  44. File wallpaperDirectory = new File("/sdcard/" + string + "/");
  45. wallpaperDirectory.mkdirs();
  46. }
  47.  
  48. File file = new File(new File("/sdcard/" + string + "/"), fileName);
  49. if (file.exists()) {
  50. file.delete();
  51. }
  52. try {
  53. FileOutputStream out = new FileOutputStream(file);
  54. imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
  55. out.flush();
  56. out.close();
  57.  
  58.  
  59. ContentValues image = new ContentValues();
  60. image.put(TITLE, fileName);
  61. image.put(DISPLAY_NAME, fileName);
  62. image.put(DESCRIPTION, "CGX");
  63. image.put(DATE_ADDED, System.currentTimeMillis());
  64. image.put(MIME_TYPE, "image/jpg");
  65. image.put(ORIENTATION, 0);
  66. File parent = file.getParentFile();
  67. image.put(MediaStore.Images.ImageColumns.BUCKET_ID, parent.toString()
  68. .toLowerCase().hashCode());
  69. image.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, parent.getName()
  70. .toLowerCase());
  71. image.put(SIZE, file.length());
  72. image.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
  73. Uri result = context.getContentResolver().insert(
  74. EXTERNAL_CONTENT_URI, image);
  75. } catch (Exception e) {
  76. e.printStackTrace();
  77. }
  78.  
  79. return file;
  80.  
  81.  
  82. }
  83.  
  84. public static Bitmap getBitmap(ImageView imageView) {
  85. BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
  86. Bitmap bitmap = drawable.getBitmap();
  87. return bitmap;
  88. }
  89.  
  90. /*public static void saveMyImage(String imageName, Bitmap imageUrl) {
  91.  
  92. Bitmap bmImg = imageUrl;
  93. File filename;
  94. try {
  95. String path1 = android.os.Environment.getExternalStorageDirectory()
  96. .toString();
  97. File file = new File(path1 + "/" + appName);
  98. if (!file.exists())
  99. file.mkdirs();
  100. filename = new File(file.getAbsolutePath() + "/" + imageName
  101. + ".jpg");
  102. FileOutputStream out = new FileOutputStream(filename);
  103. bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
  104. out.flush();
  105. out.close();
  106. ContentValues image = new ContentValues();
  107. image.put(TITLE, appName);
  108. image.put(DISPLAY_NAME, imageName);
  109. image.put(DESCRIPTION, "App Image");
  110. image.put(DATE_ADDED, System.currentTimeMillis());
  111. image.put(MIME_TYPE, "image/jpg");
  112. image.put(ORIENTATION, 0);
  113. File parent = filename.getParentFile();
  114. image.put(MediaStore.Images.ImageColumns.BUCKET_ID, parent.toString()
  115. .toLowerCase().hashCode());
  116. image.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, parent.getName()
  117. .toLowerCase());
  118. image.put(SIZE, filename.length());
  119. image.put(MediaStore.Images.Media.DATA, filename.getAbsolutePath());
  120. Uri result = getContentResolver().insert(
  121. EXTERNAL_CONTENT_URI, image);
  122. Toast.makeText(getApplicationContext(),
  123. "File is Saved in " + filename, Toast.LENGTH_SHORT).show();
  124. } catch (Exception e) {
  125. e.printStackTrace();
  126. }
  127.  
  128. }*/
  129.  
  130. }
Add Comment
Please, Sign In to add comment