Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. package com.github.example;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.net.Uri;
  6. import android.text.TextUtils;
  7. import android.util.Log;
  8.  
  9. import androidx.annotation.Nullable;
  10. import androidx.core.content.FileProvider;
  11.  
  12. import com.github.example.App;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17.  
  18. /**
  19. * Save a file in the App cache and get Uri for it
  20. *
  21. * @author Ivan V on 12.05.2019.
  22. * @version 1.0
  23. */
  24. public class Cache {
  25.  
  26. public static final String TAG = Cache.class.getSimpleName();
  27.  
  28. private static final String CHILD_DIR = "images";
  29. private static final String TEMP_FILE_NAME = "img";
  30. private static final String FILE_EXTENSION = ".png";
  31.  
  32. private static final int COMPRESS_QUALITY = 100;
  33.  
  34. /**
  35. * Save image to the App cache
  36. * @param bitmap to save to the cache
  37. * @param name file name in the cache.
  38. * If name is null file will be named by default {@link #TEMP_FILE_NAME}
  39. * @return file dir when file was saved
  40. */
  41. public File saveImgToCache(Bitmap bitmap, @Nullable String name) {
  42. File cachePath = null;
  43. String fileName = TEMP_FILE_NAME;
  44. if (!TextUtils.isEmpty(name)) {
  45. fileName = name;
  46. }
  47. try {
  48. cachePath = new File(App.getAppContext().getCacheDir(), CHILD_DIR);
  49. cachePath.mkdirs();
  50.  
  51. FileOutputStream stream = new FileOutputStream(cachePath + "/" + fileName + FILE_EXTENSION);
  52. bitmap.compress(Bitmap.CompressFormat.PNG, COMPRESS_QUALITY, stream);
  53. stream.close();
  54. } catch (IOException e) {
  55. Log.e(TAG, "saveImgToCache error: " + bitmap, e);
  56. }
  57. return cachePath;
  58. }
  59.  
  60. /**
  61. * Save an image to the App cache dir and return it {@link Uri}
  62. * @param bitmap to save to the cache
  63. */
  64. public Uri saveToCacheAndGetUri(Bitmap bitmap) {
  65. return saveToCacheAndGetUri(bitmap, null);
  66. }
  67.  
  68. /**
  69. * Save an image to the App cache dir and return it {@link Uri}
  70. * @param bitmap to save to the cache
  71. * @param name file name in the cache.
  72. * If name is null file will be named by default {@link #TEMP_FILE_NAME}
  73. */
  74. public Uri saveToCacheAndGetUri(Bitmap bitmap, @Nullable String name) {
  75. File file = saveImgToCache(bitmap, name);
  76. return getImageUri(file, name);
  77. }
  78.  
  79. /**
  80. * Get a file {@link Uri}
  81. * @param name of the file
  82. * @return file Uri in the App cache or null if file wasn't found
  83. */
  84. @Nullable public Uri getUriByFileName(String name) {
  85. Context context = App.getAppContext();
  86. String fileName;
  87. if (!TextUtils.isEmpty(name)) {
  88. fileName = name;
  89. } else {
  90. return null;
  91. }
  92.  
  93. File imagePath = new File(context.getCacheDir(), CHILD_DIR);
  94. File newFile = new File(imagePath, fileName + FILE_EXTENSION);
  95. return FileProvider.getUriForFile(context, context.getPackageName() + ".provider", newFile);
  96. }
  97.  
  98. // Get an image Uri by name without extension from a file dir
  99. private Uri getImageUri(File fileDir, @Nullable String name) {
  100. Context context = App.getAppContext();
  101. String fileName = TEMP_FILE_NAME;
  102. if (!TextUtils.isEmpty(name)) {
  103. fileName = name;
  104. }
  105. File newFile = new File(fileDir, fileName + FILE_EXTENSION);
  106. return FileProvider.getUriForFile(context, context.getPackageName() + ".provider", newFile);
  107. }
  108.  
  109. /**
  110. * Get Uri type by {@link Uri}
  111. */
  112. public String getContentType(Uri uri) {
  113. return App.getAppContext().getContentResolver().getType(uri);
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement