Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. /*
  2. Sample Parameter:
  3. String: "file:///storage/emulated/0/Android/data/com.example.disappeardents/cache/roof_1.jpg"
  4. */
  5. fun uriStringToBitmap(uriString : String) : Bitmap {
  6.  
  7. val uri = uriString.toUri()
  8. val uriPath = uri.path
  9. val bitmap = BitmapFactory.decodeFile(uriPath)
  10.  
  11. return bitmap
  12.  
  13. }
  14.  
  15. /*
  16. Sample Parameter:
  17. 1: Bitmap: [bitmap]
  18. 2: String: "my_file_name.jpg"
  19. */
  20. fun bitmapToUriString(bitmap: Bitmap, fileName: String) : Uri{
  21.  
  22. // set directory where it will be stored (on cache)
  23. var file = File(activity!!.externalCacheDir, fileName)
  24.  
  25. // save the image on cache
  26. var fOut = FileOutputStream(file)
  27. bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fOut)
  28.  
  29. // get the path of the image
  30. var uriPath = Uri.fromFile(file)
  31.  
  32. return uriPath
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement