这是代码:
package com.example.def5ine;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FileReader fileReader;
try {
Log.d("dd", "msg");
//FileReader("/home/likewise-open/vigneshwaranm/workspace/java/src/example/dictionary.json")
fileReader = new FileReader("/android_asset/dictionary.json");
jsonReader = new JsonReader(fileReader);
jsonReader.beginObject();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Button srch=(Button)findViewById(R.id.button1);
final TextView res=(TextView)findViewById(R.id.textView1);
srch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (name.equals("domain")) {
res.setText("ss");
} else {
jsonReader.skipValue();
res.setText("fail");
}
}
jsonReader.endObject();
jsonReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Log Cat输出:
12-12 07:12:26.600: D/dd(1359): msg
<b>12-12 07:12:26.600: W/System.err(1359): java.io.FileNotFoundException: /android_asset/dictionary.json: open failed: ENOENT (No such file or directory)
12-12 07:12:26.610: W/System.err(1359): at </b>libcore.io.IoBridge.open(IoBridge.java:409)
12-12 07:12:26.610: W/System.err(1359): at java.io.FileInputStream.<init>(FileInputStream.java:78)
12-12 07:12:26.610: W/System.err(1359): at java.io.FileInputStream.<init>(FileInputStream.java:105)
12-12 07:12:26.610: W/System.err(1359): at java.io.FileReader.<init>(FileReader.java:66)
12-12 07:12:26.620: W/System.err(1359): at com.example.def5ine.MainActivity.onCreate(MainActivity.java:30)
12-12 07:12:26.620: W/System.err(1359): at android.app.Activity.performCreate(Activity.java:5243)
12-12 07:12:26.620: W/System.err(1359): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-12 07:12:26.620: W/System.err(1359): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-12 07:12:26.620: W/System.err(1359): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-12 07:12:26.620: W/System.err(1359): at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-12 07:12:26.620: W/System.err(1359): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-12 07:12:26.620: W/System.err(1359): at android.os.Handler.dispatchMessage(Handler.java:102)
12-12 07:12:26.620: W/System.err(1359): at android.os.Looper.loop(Looper.java:137)
12-12 07:12:26.620: W/System.err(1359): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-12 07:12:26.620: W/System.err(1359): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 07:12:26.620: W/System.err(1359): at java.lang.reflect.Method.invoke(Method.java:515)
12-12 07:12:26.620: W/System.err(1359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-12 07:12:26.620: W/System.err(1359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-12 07:12:26.620: W/System.err(1359): at dalvik.system.NativeStart.main(Native Method)
12-12 07:12:26.620: W/System.err(1359): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
12-12 07:12:26.640: W/System.err(1359): at libcore.io.Posix.open(Native Method)
12-12 07:12:26.640: W/System.err(1359): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
12-12 07:12:26.640: W/System.err(1359): at libcore.io.IoBridge.open(IoBridge.java:393)
12-12 07:12:26.640: W/System.err(1359): ... 18 more
12-12 07:12:26.900: D/(1359): HostConnection::get() New Host Connection established 0xb85fd238, tid 1359
12-12 07:12:26.950: W/EGL_emulation(1359): eglSurfaceAttrib not implemented
12-12 07:12:26.960: D/OpenGLRenderer(1359): Enabling debug mode 0
12-12 07:12:27.140: W/IInputConnectionWrapper(1359): showStatusIcon on inactive InputConnection
12-12 07:12:51.980: D/AndroidRuntime(1359): Shutting down VM
12-12 07:12:51.980: W/dalvikvm(1359): threadid=1: thread exiting with uncaught exception (group=0xb4a5bb90)
12-12 07:12:51.990: E/AndroidRuntime(1359): FATAL EXCEPTION: main
12-12 07:12:51.990: E/AndroidRuntime(1359): Process: com.example.def5ine, PID: 1359
12-12 07:12:51.990: E/AndroidRuntime(1359): java.lang.NullPointerException
12-12 07:12:51.990: E/AndroidRuntime(1359): at com.example.def5ine.MainActivity$1.onClick(MainActivity.java:46)
12-12 07:12:51.990: E/AndroidRuntime(1359): at android.view.View.performClick(View.java:4424)
12-12 07:12:51.990: E/AndroidRuntime(1359): at android.view.View$PerformClick.run(View.java:18383)
12-12 07:12:51.990: E/AndroidRuntime(1359): at android.os.Handler.handleCallback(Handler.java:733)
12-12 07:12:51.990: E/AndroidRuntime(1359): at android.os.Handler.dispatchMessage(Handler.java:95)
12-12 07:12:51.990: E/AndroidRuntime(1359): at android.os.Looper.loop(Looper.java:137)
12-12 07:12:51.990: E/AndroidRuntime(1359): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-12 07:12:51.990: E/AndroidRuntime(1359): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 07:12:51.990: E/AndroidRuntime(1359): at java.lang.reflect.Method.invoke(Method.java:515)
12-12 07:12:51.990: E/AndroidRuntime(1359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-12 07:12:51.990: E/AndroidRuntime(1359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-12 07:12:51.990: E/AndroidRuntime(1359): at dalvik.system.NativeStart.main(Native Method)
12-12 07:13:03.090: I/Process(1359): Sending signal. PID: 1359 SIG: 9
答案 0 :(得分:0)
注意由于这个问题有一些观点,我认为这对其他人有帮助,我会回答我自己的问题。
要访问资产目录,您必须在目录名前面加上文件:///前缀:
在我的情况下:
String pathToMyAssetDirectory="file:///android_asset/dictionary.json";
File file= new File(pathToMyAssetDirectory);
获取资产目录引用的另一个选项是使用应用程序Context,如下所示:
context.getResources().getAssets().open("fileName", Context.MODE_PRIVATE);