Tag: 科林

调用File.createNewFile()时,权限被拒绝 Android的

我试图创建一个图像文件到外部存储来分享它。 但在尝试下面的代码时,我有一些错误 var icon: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.namelogo) val shareIntent = Intent(Intent.ACTION_SEND) shareIntent.type = "image/*" val bytes = ByteArrayOutputStream() icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes) val path = File.separator + "temporary_file.jpg" val f = File(Environment.getExternalStorageDirectory().toString(), path) try { if (!f.parentFile.exists()) f.parentFile.mkdirs() if (!f.exists()) f.createNewFile() f.createNewFile() val fo = FileOutputStream(f) fo.write(bytes.toByteArray()) } catch (e: IOException) { Log.e("path = ", […]

我想从文件夹打开文件

//这段代码帮助我列出所有的文件夹。 我想打开这些文件夹中的文件,并执行操作,如播放,而不显示这些文件夹内的内容。 任何形式的帮助是值得欢迎的。 我是新来的机器人,请帮助 val root = LinearLayout(this) val file = File("/sdcard/MedProRecordings/").absoluteFile if (file.isDirectory == false) { Toast.makeText(this, "ERROR", Toast.LENGTH_SHORT).show() } val files = file.listFiles() var i = 1 for (f in files!!) { if (f.isFile || f.isDirectory) { try { val layout = LinearLayout(this) layout.id = i val text = Button(this) text.setText(f.name) text.setMinWidth(400) layout.addView(text) […]

注册反序列化器以包装类型的List

我有这样的JSON: { "apps": [ { "id": "1", … }, { "id": "2", … } ] } 例如说, Application类看起来像这样 data class Application( val id: String ) 我想将JSON反序列化为List<Application> ,其中每个{…}都是一个Application 。 我希望能够做到这一点,而不必创建一个类似Applications的包装类,用@JsonRootName对其进行@JsonRootName ,然后启用DeserializationFeature.UNWRAP_ROOT_VALUE 。 最终目标是有一个类似于以下内容的Retrofit界面: @GET("api/apps") fun listApplications(): Call<List<Application>> 我试图实现一个简单的JsonDeserializer (可能可以优化): class ApplicationListDeserializer : JsonDeserializer<List<Application>>() { companion object { private val COLLECTION_TYPE: CollectionType = TypeFactory.defaultInstance() .constructCollectionType(List::class.java, Application::class.java) } […]

Android RecyclerView使用Kotlin处理数据:修改后的行切换索引

我正在Kotlin编写我的第一个Android项目,并与recyclerView很难。 MutableList <String>用作我的适配器的数据源,并具有编辑模式和正常模式。 onCreateViewHolder()用内部的editText膨胀一个视图,然后创建一个视图持有者来保存这个视图。 ViewHolder有一个包含我的逻辑的函数bindSkill()。 onBindViewHolder()调用viewHolder.bindSkill() bindSkill()具有在EditMode和Normal Mode之间转换的逻辑。 当我将EditMode设置为true时,应该发生三件事情: EditText字段应该设置为.isEnable = true(允许用户编辑文本字段) 应该将EditText字段的背景设置为.background = R.drawable.m_gray_border(向用户显示文本字段) 从mStringMutableList加载文本(如果需要?) 当再次按下时,倒过程应该运行(有一些修改: EditText字段应该设置为.isEnable = false EditText字段应该将背景设置为.background = null(隐藏文本字段边框) 保存来自mStringMutableList的文本(如果需要?) 使用•%1 $ s格式化字符串以制作项目符号列表 问题 当用我的数据加载适配器时,它将被编辑文本字段中的空字符串覆盖。 另外,当我进入编辑模式,并更改文本字段的recycleView行开始切换的地方。 文本按预期更改并存储和格式化,但会移至新索引。 如果你填写所有的文本字段,他们将被颠倒,但是如果你只改变了几个,他们就会随机地改变位置。 这种问题有没有普遍的做法? 它接缝很简单,但我不能弄明白。 任何帮助表示赞赏! 代码 活动: class ProfileActivity : BaseActivity() { private lateinit var recyclerViewProfile: RecyclerView private lateinit var linearLayoutManager: LinearLayoutManager private lateinit var […]

Kotlin公司的“分区”的首选解决方案是什么?为什么?

我通过Kotlin koan分区工作,结果通过了单元测试: fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> = customers.filter{it.orders.partition{it.isDelivered} .let{(delivered, undelivered) -> delivered.size < undelivered.size}}.toSet() 这是解决方案的变体: fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> = customers.filter { val (delivered, undelivered) = it.orders.partition { it.isDelivered } undelivered.size > delivered.size }.toSet() 有什么理由选择这些解决方案之一吗?