如何从onActivityResult中获取字节码?

问题描述 投票:0回答:1

我想得到这样的东西。

...2 47 ef bf bd cb 92 3a 53 0e 5e ef bf bd ef bf bd d6...

根据我的理解,这是文件的字符串内容被转换成字节。我试过这样的方法。

override fun onActivityResult(requestCode: Int, resultCode: Int, result: Intent?) {
        super.onActivityResult(requestCode, resultCode, result)
        when {
            requestCode == 1 && resultCode == Activity.RESULT_OK -> {
                if (result != null) {
                    val documentFile: DocumentFile = DocumentFile.fromSingleUri(this, result.data!!)!!

                    val hereUrl: Uri? = result.data
                    val inputStream = this.contentResolver.openInputStream(hereUrl!!)
                    val byteArray = inputStream!!.readBytes().contentToString()


                    Timber.i(byteArray.toUtf8Bytes().toString())
                }
            }
       }
}

但结果是我在logcat上收到了这样的输出:

[B@70da221

那么,如何得到这样的数字和字母的组合?

更新

我创建了这样的函数。

fun byteToHex(num: ByteArray): ArrayList<String> {
        val stringArray = ArrayList<String>()
        for (i in num.indices){
            val hexDigits = CharArray(2)
            hexDigits[0] = Character.forDigit(i shl 4 and 0xF, 16)
            hexDigits[1] = Character.forDigit(i and 0xF, 16)
            stringArray.add(String(hexDigits))
        }


        return stringArray
    }

并收到这样的输出:

00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 00, 

但我看到的是,有些东西出了问题 :(

android onactivityresult
1个回答
0
投票

如果你想把你的字节数组显示在 String 格式,那么你必须使用 Arrays 转换 byteArrayString 像下面这样。

Timber.i(Arrays.toString(byteArray))

为此,你必须使用下面的函数将每个字节转换为Hax。

爪哇

public String byteToHex(byte num) {
    char[] hexDigits = new char[2];
    hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);
    hexDigits[1] = Character.forDigit((num & 0xF), 16);
    return new String(hexDigits);
}
© www.soinside.com 2019 - 2024. All rights reserved.