首页 > 代码库 > 通过assets像安卓程序目录中导入数据库文件出现错误的解决办法

通过assets像安卓程序目录中导入数据库文件出现错误的解决办法

错误提示如下,下面的错误信息

08-13 07:36:31.396: W/System.err(9814): java.io.IOException

08-13 07:36:31.416: W/System.err(9814): at android.content.res.AssetManager.readAsset(Native Method)
08-13 07:36:31.416: W/System.err(9814):  at android.content.res.AssetManager.access$700(AssetManager.java:36)
08-13 07:36:31.426: W/System.err(9814):  at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
08-13 07:36:31.426: W/System.err(9814):  at com.wzw.mobilesafe.SplashActivity.copyDB(SplashActivity.java:96)
08-13 07:36:31.426: W/System.err(9814):  at com.wzw.mobilesafe.SplashActivity.onCreate(SplashActivity.java:63)
08-13 07:36:31.426: W/System.err(9814):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-13 07:36:31.426: W/System.err(9814):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-13 07:36:31.426: W/System.err(9814):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-13 07:36:31.426: W/System.err(9814):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-13 07:36:31.426: W/System.err(9814):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-13 07:36:31.436: W/System.err(9814):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 07:36:31.436: W/System.err(9814):  at android.os.Looper.loop(Looper.java:123)
08-13 07:36:31.436: W/System.err(9814):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-13 07:36:31.436: W/System.err(9814):  at java.lang.reflect.Method.invokeNative(Native Method)
08-13 07:36:31.436: W/System.err(9814):  at java.lang.reflect.Method.invoke(Method.java:521)
08-13 07:36:31.446: W/System.err(9814):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-13 07:36:31.446: W/System.err(9814):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-13 07:36:31.446: W/System.err(9814):  at dalvik.system.NativeStart.main(Native Method)
08-13 07:36:31.446: I/System.out(9814): 异常了


看下我们的主要代码


			File file=new File(getFilesDir(), "address.db");
			InputStream is= getAssets().open("address.db");
			FileOutputStream fos=new FileOutputStream(file);
			byte buffer[]=new byte[1024];
			int len=0;
			while((len=is.read(buffer))>0){
				fos.write(buffer, 0, len);
			}
			fos.flush();
			is.close();
			fos.close();


解决办法:

把assets目录下的文件改后缀名,如mp3.就可以解决了。。


遇见这样的问题比较奇葩

好像数据库文件大小有限制。


08-13 07:36:31.396: W/System.err(9814): java.io.IOException

08-13 07:36:31.416: W/System.err(9814): at android.content.res.AssetManager.readAsset(Native Method)
08-13 07:36:31.416: W/System.err(9814):  at android.content.res.AssetManager.access$700(AssetManager.java:36)
08-13 07:36:31.426: W/System.err(9814):  at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
08-13 07:36:31.426: W/System.err(9814):  at com.wzw.mobilesafe.SplashActivity.copyDB(SplashActivity.java:96)
08-13 07:36:31.426: W/System.err(9814):  at com.wzw.mobilesafe.SplashActivity.onCreate(SplashActivity.java:63)
08-13 07:36:31.426: W/System.err(9814):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-13 07:36:31.426: W/System.err(9814):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-13 07:36:31.426: W/System.err(9814):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-13 07:36:31.426: W/System.err(9814):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-13 07:36:31.426: W/System.err(9814):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-13 07:36:31.436: W/System.err(9814):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 07:36:31.436: W/System.err(9814):  at android.os.Looper.loop(Looper.java:123)
08-13 07:36:31.436: W/System.err(9814):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-13 07:36:31.436: W/System.err(9814):  at java.lang.reflect.Method.invokeNative(Native Method)
08-13 07:36:31.436: W/System.err(9814):  at java.lang.reflect.Method.invoke(Method.java:521)
08-13 07:36:31.446: W/System.err(9814):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-13 07:36:31.446: W/System.err(9814):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-13 07:36:31.446: W/System.err(9814):  at dalvik.system.NativeStart.main(Native Method)
08-13 07:36:31.446: I/System.out(9814): 异常了