Created
May 24, 2020 23:38
-
-
Save emedinaa/5fc707544b9c1e2cbe2e6d26eaffcb57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.emedinaa.rawdemo | |
import android.net.Uri | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.appcompat.app.AppCompatActivity | |
import kotlinx.android.synthetic.main.activity_main.* | |
import java.io.* | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
button.setOnClickListener { | |
loadTextFromRaw() | |
} | |
} | |
private fun loadTextFromRaw(){ | |
val file = File(Uri.parse("android.resource://${packageName}/${R.raw.demo}").toString()) | |
Log.i("CONSOLE","file $file") //file android.resource:/com.emedinaa.rawdemo/2131492864 | |
val stringBuilder = StringBuilder() | |
val inputStream: InputStream = this.resources.openRawResource(R.raw.demo) | |
val reader = BufferedReader(InputStreamReader(inputStream)) | |
var string = "" | |
while (true) { | |
try { | |
val line = reader.readLine() | |
line?.let { | |
string = line | |
} | |
if(line == null) break | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
stringBuilder.append(string).append("\n") | |
} | |
inputStream.close() | |
textView.text = stringBuilder.toString() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment