package com.sw.dualscreen.utils import android.content.Context import android.graphics.Bitmap import android.graphics.BitmapFactory import java.io.BufferedReader import java.io.IOException import java.io.InputStreamReader object AssetsTool { // fun readJson(context: Context, fileName: String): List { // val json = readAssetsFile(context, fileName) // val json2 = json.replace("[[", "[").replaceAfterLast("]]", "]") // val list = json2.split("],") // val resultList = mutableListOf() // var count = 0 // val tempList = mutableListOf() // list.forEachIndexed { index, text -> // if (count >= 500) { // val tempJson = "[${tempList.joinToString (",")}]" // resultList.add(tempJson) // tempList.clear() // count = 0 // } // val newText = if (index == list.size - 1) text else "${text}]" // tempList.add(newText) // count++ // } // if (count < 500) { // val tempJson = "[${tempList.joinToString(",")}]" // resultList.add(tempJson) // } // return resultList // } fun readAssetsFile(context: Context, fileName: String): String { val stringBuilder = StringBuilder() try { val bf = BufferedReader(InputStreamReader(context.assets.open(fileName))) bf.useLines { lines -> lines.forEach { stringBuilder.append(it) } } } catch (e: IOException) { e.printStackTrace() } return stringBuilder.toString() } fun loadImagesFromAssets(context: Context, subPath: String): MutableList { val bitmaps: MutableList = mutableListOf() val assetManager = context.assets try { val files = assetManager.list(subPath) files?.forEach { file -> assetManager.open("$subPath/$file").use { `is` -> val bitmap = BitmapFactory.decodeStream(`is`) if (bitmap != null) { bitmaps.add(bitmap) } } } } catch (e: IOException) { e.printStackTrace() } return bitmaps } fun loadImageBitmapFromAssets(context: Context, imagePath:String, action:(bmp: Bitmap)-> Unit) { context.assets.open(imagePath).use { `is` -> val bitmap = BitmapFactory.decodeStream(`is`) action(bitmap) } } }