我正在嘗試獲取.txt我res/raw檔案夾中檔案的路徑,但我一直得到一個NoSuchFileException,我不確定我做錯了什么。
public static final String PATH_TO_BINARY_CONTRACT = "android.resource://com.example.testproject/raw/binary_code";
Uri uri = Uri.parse(Constants.PATH_TO_BINARY_CONTRACT);
try {
byte[] encoded = Files.readAllBytes(Paths.get(uri.getPath()));
}catch (Exception e){
Log.e(TAG, "onCreate: ", e);
}
這不斷拋出一個 java.nio.file.NoSuchFileException: /raw/binary_code
我做錯了什么?
uj5u.com熱心網友回復:
我正在嘗試獲取 res/raw 檔案夾中 .txt 檔案的路徑
沒有路徑。資源是開發機器上的檔案。它不是設備上的檔案。
要在原始資源的內容讀出,呼叫openRawResource()一個上Resources物件。您可以Resources通過呼叫getResources()any 來獲得一個物件Context。
uj5u.com熱心網友回復:
嘗試這個:
Uri pathUri = new Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(context.getPackageName())
.path(String.valueOf(R.raw.binary_code))
.build();
然后使用這個:
File file = new File(pathUri.getPath()); //create path from uri
但我仍然不明白你為什么要這樣做。我還沒有嘗試過這個。但是我在 res/raw 檔案夾中得到了一個 .ogg 檔案的 Uri 像這樣。但它應該適合你。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/330651.html
標籤:安卓
上一篇:測驗僅使用其他服務類方法的服務類
