Advertisement
Guest User

Untitled

a guest
Nov 27th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.67 KB | None | 0 0
  1. class Fr7downPage : Fragment() {
  2.  
  3.  
  4.     override fun onCreateView(
  5.         inflater: LayoutInflater, container: ViewGroup?,
  6.         savedInstanceState: Bundle?): View? {
  7.  
  8.         val textFont = Typeface.createFromAsset(activity!!.assets, "text.ttf")
  9.  
  10.         val inflView =  inflater.inflate(R.layout.fragment_fr7down_page, container, false)
  11.  
  12.         val downTitle: TextView? = inflView.findViewById(R.id.downTitle)
  13.         val downDesc: TextView? = inflView.findViewById(R.id.downDesc)
  14.         val downBtn: Button? = inflView.findViewById(R.id.downBtn)
  15.         val rateBtn: Button? = inflView.findViewById(R.id.rateBtn)
  16.  
  17.         downTitle!!.typeface = textFont
  18.         downDesc!!.typeface = textFont
  19.         downBtn!!.typeface = textFont
  20.         rateBtn!!.typeface = textFont
  21.  
  22.         rateBtn.setOnClickListener {
  23.             val intent = Intent(Intent.ACTION_VIEW)
  24.             intent.data = Uri.parse("market://details?id=master.mods.download.mcpe.mods.furnituremodsforminecraft")
  25.             startActivity(intent)
  26.         }
  27.  
  28.         downBtn.setOnClickListener {
  29.             copyFileAssets("file.zip")
  30.         }
  31.  
  32.         return inflView
  33.     }
  34.  
  35.     private fun copyFileAssets(filename: String) {
  36.         val dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath
  37.         val dir = File(dirPath)
  38.         if (!dir.exists()) {
  39.             dir.mkdirs()
  40.         }
  41.  
  42.         val assetManager = assets
  43.         var inPut: InputStream? = null
  44.         var outPut: OutputStream? = null
  45.  
  46.         try {
  47.             inPut = assetManager.open(filename)
  48.             val outFile = File(dirPath, filename)
  49.             outPut = FileOutputStream(outFile)
  50.             copyFile(inPut!!, outPut)
  51.             Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show()
  52.         } catch (e: IOException) {
  53.             e.printStackTrace()
  54.             Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show()
  55.         } finally {
  56.             if (inPut != null) {
  57.                 try {
  58.                     inPut.close()
  59.                 } catch (e: IOException) {
  60.                     e.printStackTrace()
  61.                 }
  62.             }
  63.             if (outPut != null) {
  64.                 try {
  65.                     outPut.close()
  66.                 } catch (e: IOException) {
  67.                     e.printStackTrace()
  68.                 }
  69.             }
  70.         }
  71.     }
  72.  
  73.     @Throws(IOException::class)
  74.     fun copyFile(inPut: InputStream, outPut: OutputStream) {
  75.  
  76.         val buffer = ByteArray(1024)
  77.         var read: Int
  78.  
  79.         while ((inPut.read(buffer).let{read = it; it!= -1}) ) {
  80.             outPut.write(buffer, 0, read)
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement