Skip to content

Instantly share code, notes, and snippets.

@davidliu
Created March 27, 2019 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidliu/dbebbe0a58a5035ecd541b573518c6cf to your computer and use it in GitHub Desktop.
Save davidliu/dbebbe0a58a5035ecd541b573518c6cf to your computer and use it in GitHub Desktop.
Random access through SAF
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.type = "*/*"
intent.addCategory(Intent.CATEGORY_OPENABLE)
startActivityForResult(intent, 1)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == 1 && resultCode == RESULT_OK) {
if (data != null) {
val uri = data.data
val pfd = contentResolver.openFileDescriptor(uri, "r")
val inputStream = FileInputStream(pfd.fileDescriptor)
inputStream.skip(995)
inputStream.skip(-3)
for (i in 1..10) {
val byte = inputStream.read()
if (byte == -1) {
break;
}
Log.v("LOL", "${byte.toChar()}");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment