我有一個(gè) API 級(jí)別為 18 的 android 項(xiàng)目。當(dāng)我將路徑轉(zhuǎn)換為 File (with .toFile()) 時(shí),它說需要 API 級(jí)別 26 及以上。那么如何將 java nio 轉(zhuǎn)換path為File可靠的方式?這是來源OutputStream createStream(Path filepath){ File file = filepath.toFile() // <--- android studio says need api level 26 return new FileOutPutStream(file)}來自 android 開發(fā)者網(wǎng)站API 26 中添加的 java.nio.file.Files
1 回答

慕田峪9158850
TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以使用 的toString()方法Path。
OutputStream createStream(Path filepath){
File file = new File(filepath.toString());
return new FileOutPutStream(file)
}
String toString() 返回此路徑的字符串表示形式。如果此路徑是通過使用 getPath 方法轉(zhuǎn)換路徑字符串創(chuàng)建的,則此方法返回的路徑字符串可能與用于創(chuàng)建路徑的原始字符串不同。
返回的路徑字符串使用默認(rèn)名稱分隔符來分隔路徑中的名稱。
添加回答
舉報(bào)
0/150
提交
取消