roko_man

Gallery Image To file

Jan 14th, 2018
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.66 KB | None | 0 0
  1.    val selectedImage = data?.getData()
  2.             val myPath :String = ImageFilePath.getPath(this@MainActivity, selectedImage)
  3.             val bitmap : Bitmap = flipImage(myPath)
  4.             doAsync {
  5.                 //========== create file =======================
  6.                 val random = Random()
  7.                 val randomN = random.nextInt(1421200000)
  8.                 //create a file to write bitmap data
  9.                 filez = File(this@MainActivity.getCacheDir(), "image" + bitmap.getByteCount() + "" + randomN + ".png")
  10.                 filez?.createNewFile()
  11.                 //Convert bitmap to byte array
  12.                 //  Bitmap bitmap = your bitmap;
  13.                 val bos = ByteArrayOutputStream()
  14.                 bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos)
  15.                 val bitmapdata = bos.toByteArray()
  16.                 //write the bytes in file
  17.                 val fos = FileOutputStream(filez)
  18.                 fos.write(bitmapdata)
  19.                 fos.flush()
  20.                 fos.close()
  21.                 uiThread {
  22.  
  23.                     Glide.with(this@MainActivity).load(filez).into(dialogUserImg);
  24.                     //upload image
  25.                     MyApplicationClass.instance.showProgressBar(act)
  26.                     presenter?.interactor?.uploadImage(file = filez!!,activity = act,repeatTryingIfThereIsErrorOnServer = true, callBack = object : MainActivityInteractorInterface.cancelOrderSucess{
  27.                         override fun clickHere() {
  28.                             MyApplicationClass.instance.progressDialog.dismiss()
  29.                         }
  30.                     });
  31.                 }
  32.             }
Advertisement