You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中File.createTempFile抛出IOException问题求助

问题:Android API 33模拟器中无法创建磁盘文件

已被该问题困扰约16小时,查阅了各类资料,尝试替换为createNewFile() API也无效,始终无法定位原因,恳请帮助。

代码片段

fun createImgFile(context: Context): File? {
    return try {
        val fileTimeStamp: String = SimpleDateFormat("yyyy/MM/dd").format(Date())
        val fileName = "MyImg_$fileTimeStamp"
        File.createTempFile(fileName, null, context.cacheDir)
    } catch (exception: IOException) {
        exception.printStackTrace()
        null
    }
}

环境与现象

  • 传入的Context为Composable函数中的LocalContext.current
  • 运行环境:Pixel3a模拟器(Android API 33)
  • 现象:仅创建内存中的文件引用时可正常工作,但创建实际磁盘文件时失败

内容的提问来源于stack exchange,提问作者Sdr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 23:33:09