我是与Java一起在Android中的新手编程(实际上我是Java中的新编程)。
这是我的问题:我正在尝试从位于" src/main/res/raw/data.dat"的源代码打开项目资源文件。这是我当前的代码:
try
{
DataInputStream stm = new DataInputStream(this.getAssets().open("res/raw/data.dat"));
Log.d("out",String.valueOf(stm.readUnsignedShort()));
Log.d("out",String.valueOf(stm.readInt()));
stm.close();
}catch (Exception ex)
{
ex.printStackTrace();
}
另外,我尝试使用" RAW/DATA.DAT"," DATA.DAT"," SRC/MAIN/RES/RES/RAW/RAW/DATA.DAT"one_answers"拨打this.getAssets()。open()。open()。main/res/raw/data.dat",始终返回相同的错误:
01-23 22:40:40.719 13233-13233/com.rsoft.remotesystem W/System.err﹕ java.io.FileNotFoundException: <file>
01-23 22:40:40.729 13233-13233/com.rsoft.remotesystem W/System.err﹕ at android.content.res.AssetManager.openAsset(Native Method)
01-23 22:40:40.729 13233-13233/com.rsoft.remotesystem W/System.err﹕ at android.content.res.AssetManager.open(AssetManager.java:316)
01-23 22:40:40.729 13233-13233/com.rsoft.remotesystem W/System.err﹕ at android.content.res.AssetManager.open(AssetManager.java:290)
01-23 22:40:40.729 13233-13233/com.rsoft.remotesystem W/System.err﹕ at com.rsoft.remotesystem.MainActivity.connect(MainActivity.java:50)
01-23 22:40:40.729 13233-13233/com.rsoft.remotesystem W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
01-23 22:40:40.729 13233-13233/com.rsoft.remotesystem W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
01-23 22:40:40.729 13233-13233/com.rsoft.remotesystem W/System.err﹕ at android.view.View$1.onClick(View.java:3833)
01-23 22:40:40.729 13233-13233/com.rsoft.remotesystem W/System.err﹕ at android.view.View.performClick(View.java:4475)
01-23 22:40:40.729 13233-13233/com.rsoft.remotesystem W/System.err﹕ at android.view.View$PerformClick.run(View.java:18786)
01-23 22:40:40.729 13233-13233/com.rsoft.remotesystem W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730)
01-23 22:40:40.739 13233-13233/com.rsoft.remotesystem W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 22:40:40.739 13233-13233/com.rsoft.remotesystem W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
01-23 22:40:40.739 13233-13233/com.rsoft.remotesystem W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5419)
01-23 22:40:40.739 13233-13233/com.rsoft.remotesystem W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
01-23 22:40:40.739 13233-13233/com.rsoft.remotesystem W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
01-23 22:40:40.739 13233-13233/com.rsoft.remotesystem W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
01-23 22:40:40.739 13233-13233/com.rsoft.remotesystem W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
01-23 22:40:40.739 13233-13233/com.rsoft.remotesystem W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
我该怎么办?
使用getAssets()
检索的文件不在您的/res
文件夹中,而是在您的/assets
文件夹中。
将/res/raw/data.dat
文件移至/assets/data.dat
,您应该能够使用getAssets()
访问您的文件。