我使用 kotlin 的 apache poi 创建了一个 word.docx 文件,并将此文档保存到外部存储。当我尝试在此 docx 文件中添加照片时,出现错误消息“没有这样的目录”,我无法获取它
fun createFolder(): File{
val folderpath = Environment.getExternalStorageDirectory()
val folderName="VEC"
vecAppFileDirectory = File(folderpath.absolutePath,"/$folderName")
try{ if (!vecAppFileDirectory.exists()) vecAppFileDirectory.mkdirs() }
catch(e:Exception)
{ Log.e("vecAppFileDirectory", e.printStackTrace().toString()) }
return vecAppFileDirectory
}
fun createWordDoc(wordFileName: String): XWPFDocument {
vecAppFileDirectory =createFolder()
val vecWordDoc = XWPFDocument()
saveOurDoc(vecWordDoc,wordFileName)
return vecWordDoc
}
fun saveOurDoc(targetDoc: XWPFDocument, wordFileName: String){
subFolderDirectory=createSubFolder("Documents")
val wordFile=File(subFolderDirectory,"/$wordFileName")
try {
fileOutputStream = FileOutputStream(wordFile)
targetDoc.write(fileOutputStream)
fileOutputStream.close()
} catch (e: FileNotFoundException) {
Log.e("vecAppFileDirectory", e.message.toString())
} catch (e: IOException) {
Log.e("vecAppFileDirectory", e.message.toString())
}
}
我尝试将其作为路径和uri,每次都失败
private fun addImage(targetDoc: XWPFDocument, rowParagraph: XWPFTableCell, wordFileName: String) {
val picRaragraph=rowParagraph.addParagraph()
val picRun=picRaragraph.createRun()
val picture=File("D:\1- Android programming\kotlin course\kotlin projects\ValvesEngineeringCompany\app\src\main\res\drawable\veclogo.jpeg")
val picturePath=picture.absolutePath
val pictureData=FileInputStream(picturePath)
也尝试过这个
val picture= Resources.getSystem().openRawResource(R.drawable.vec_logo)
val pictureData= picture.readAllBytes()
picture.read(pictureData)
也尝试过这个
val uri= Uri.parse("android.resource://$packageName/drawable/vec_logo")
val pictureData=context.contentResolver.openInputStream(uri)?.use {
it.readBytes()
}
val picture=FileInputStream(uri.path)
“D: - Android 编程\kotlin 课程\kotlin 项目\VallvesEngineeringCompany pp\src\main es\drawable eclogo.jpeg"
这是 Windows 计算机 D: 分区上的文件。
但是您的 Android 设备在很远的城镇..
那么您的文件从哪里开始?