Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Fr7downPage : Fragment() {
- override fun onCreateView(
- inflater: LayoutInflater, container: ViewGroup?,
- savedInstanceState: Bundle?): View? {
- val textFont = Typeface.createFromAsset(activity!!.assets, "text.ttf")
- val inflView = inflater.inflate(R.layout.fragment_fr7down_page, container, false)
- val downTitle: TextView? = inflView.findViewById(R.id.downTitle)
- val downDesc: TextView? = inflView.findViewById(R.id.downDesc)
- val downBtn: Button? = inflView.findViewById(R.id.downBtn)
- val rateBtn: Button? = inflView.findViewById(R.id.rateBtn)
- downTitle!!.typeface = textFont
- downDesc!!.typeface = textFont
- downBtn!!.typeface = textFont
- rateBtn!!.typeface = textFont
- rateBtn.setOnClickListener {
- val intent = Intent(Intent.ACTION_VIEW)
- intent.data = Uri.parse("market://details?id=master.mods.download.mcpe.mods.furnituremodsforminecraft")
- startActivity(intent)
- }
- downBtn.setOnClickListener {
- copyFileAssets("file.zip")
- }
- return inflView
- }
- private fun copyFileAssets(filename: String) {
- val dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath
- val dir = File(dirPath)
- if (!dir.exists()) {
- dir.mkdirs()
- }
- val assetManager = assets
- var inPut: InputStream? = null
- var outPut: OutputStream? = null
- try {
- inPut = assetManager.open(filename)
- val outFile = File(dirPath, filename)
- outPut = FileOutputStream(outFile)
- copyFile(inPut!!, outPut)
- Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show()
- } catch (e: IOException) {
- e.printStackTrace()
- Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show()
- } finally {
- if (inPut != null) {
- try {
- inPut.close()
- } catch (e: IOException) {
- e.printStackTrace()
- }
- }
- if (outPut != null) {
- try {
- outPut.close()
- } catch (e: IOException) {
- e.printStackTrace()
- }
- }
- }
- }
- @Throws(IOException::class)
- fun copyFile(inPut: InputStream, outPut: OutputStream) {
- val buffer = ByteArray(1024)
- var read: Int
- while ((inPut.read(buffer).let{read = it; it!= -1}) ) {
- outPut.write(buffer, 0, read)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement