我正在选择多个图像并将它们发送到服务器。 从main中获取图像uri数组并在viewModel中处理。
private fun changeType(imageUriList: ArrayList<Uri>): ArrayList<MultipartBody.Part> {
var imageRequestList = ArrayList<MultipartBody.Part>()
for(i in 0 until imageUriList.size) {
val path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
val file = File(path, imageUriList[i].path.toString())
try {
if (!path.isDirectory) {
path.mkdirs()
}
file.createNewFile()
} catch (e: Exception){
e.printStackTrace()
}
val requestFile = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
val body = MultipartBody.Part.createFormData("image", file.name, requestFile)
imageRequestList.add(body)
}
return imageRequestList
}
这是视图模型
E/WritingViewModel: /storage/emulated/0/Pictures/document/image:35: open failed: ENOENT (No such file or directory)
错误提示