Android Studio中assets目录abaa.wav文件找不到异常求助
Hey there, let's break down why you're hitting this java.io.FileNotFoundException when trying to read abaa.wav from your app's assets folder, and walk through the fixes step by step.
错误日志
05-22 13:34:56.067 12052-12052/com.ringdroid D/WAVE: EXCEPTION java.io.FileNotFoundException: abaa.wav at android.content.res.AssetManager.openAsset(Native Method) at android.content.res.AssetManager.open(AssetManager.java:327) at android.content.res.AssetManager.open(AssetManager.java:301) at com.ringdroid.spectrogram.WaveTools.wavread(WaveTools.java:41) at com.ringdroid.spectrogram.SpectrogramActivity.onCreate(SpectrogramActivity.java:67) at android.app.Activity.performCreate(Activity.java:5447) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) at android.app.ActivityThread.access$800(ActivityThread.java:166) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5590) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1280) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1096) at dalvik.system.NativeStart.main(Native Method)
相关代码片段
- WaveTools.java 第41行:
is2 = mCtx.getAssets().open(path); - SpectrogramActivity.java 第67行:
audioBuf = WaveTools.wavread(inputPath, this);
WaveTools.java 完整类代码
public class WaveTools { static byte[] myData = null; public static byte[] myData2 = null; static int mySampleRate; public static float [] wavread(String path, Context mCtx) { String strThrow = "Error"; InputStream inFile = null; byte[] tmpLong = new byte[4]; byte[] tmpInt = new byte[2]; long myChunkSize; long mySubChunk1Size; int myFormat; long myChannels; long myByteRate; int myBlockAlign; int myBitsPerSample; long myDataSize = 0; float [] buffer = null; myData = null; try{ InputStream is2 = null; is2 = mCtx.getAssets().open(path); inFile = new DataInputStream(is2); String chunkID ="" + (char)inFile.read() + (char)inFile.read() + (char)inFile.read() + (char)inFile.read(); inFile.read(tmpLong); // read the ChunkSize myChunkSize = byteArrayToLong(tmpLong); String format = "" + (char)inFile.read() + (char)inFile.read() + (char)inFile.read() + (char)inFile.read(); if (!format.equals("WAVE")){ strThrow="File format is not .wav"; throw new IllegalStateException(strThrow); } //Log.d("WAVE","format = "+format); String subChunk1ID = "" + (char)inFile.read() + (char)inFile.read() + (char)inFile.read() + (char)inFile.read(); inFile.read(tmpLong); // read the SubChunk1Size mySubChunk1Size = byteArrayToLong(tmpLong); inFile.read(tmpInt); // read the audio format. This should be 1 for PCM myFormat = byteArrayToInt(tmpInt); //Log.d("WAVE","myFormat = "+myFormat); inFile.read(tmpInt); // read the # of channels (1 or 2) myChannels = byteArrayToInt(tmpInt); if (myChannels > 1){ strThrow = "File format is not mono"; throw new IllegalStateException(strThrow); } inFile.read(tmpLong); // read the samplerate mySampleRate = (int)byteArrayToLong(tmpLong); //Log.d("WAVE","channels = "+myChannels); if (mySampleRate > mySampleRate){ strThrow = "File format is not 8kHz"; throw new IllegalStateException(strThrow); } Log.d("WAVE","Fs = "+mySampleRate); inFile.read(tmpLong); // read the byterate myByteRate = byteArrayToLong(tmpLong); inFile.read(tmpInt); // read the blockalign myBlockAlign = byteArrayToInt(tmpInt); inFile.read(tmpInt); // read the bitspersample myBitsPerSample = byteArrayToInt(tmpInt); String dataChunkID = "" + (char)inFile.read() + (char)inFile.read() + (char)inFile.read() + (char)inFile.read(); inFile.read(tmpLong); // read the size of the data myDataSize = byteArrayToLong(tmpLong); // read the data chunk myData = new byte[(int)myDataSize]; myData2 = new byte[(int)myDataSize]; Short [] shortVal = new Short[(int) myDataSize/2]; ByteBuffer bb = ByteBuffer.allocateDirect(2); int max = 0; buffer = new float[(int) myDataSize/2]; bb.order(ByteOrder.LITTLE_ENDIAN); int count = 0; for (int i = 0; i<myDataSize;i+=2){ inFile.read(tmpInt); myData[i]= tmpInt[0]; myData[i+1]= tmpInt[1]; bb.position(0); bb.put(tmpInt[0]); bb.put(tmpInt[1]); buffer[count] = (float) bb.ge } } }
排查与解决方案
Let's go through the most common causes for this error, in order of likelihood:
文件名大小写不匹配
Android's assets system is case-sensitive. Double-check that your file is named exactlyabaa.wav(lowercase.wavextension, no uppercase letters in the filename). It's easy to accidentally name itABAA.wavorabaa.WAVwhich won't match the path you're passing.Assets文件夹位置不正确
Make sure yourabaa.wavis placed in thesrc/main/assetsdirectory. Android Studio uses this as the default assets directory for the main app source set. If you put it insrc/assetsortest/assets, it won't be included in the main APK.文件未被打包进APK
Sometimes files don't get bundled into the APK even if they're in the right folder. To verify:- Go to Build > Analyze APK in Android Studio
- Select your app's APK file
- Check if
abaa.wavexists under theassetsdirectory in the APK viewer
If it's missing, check your.gitignorefile to make sure it's not excluding.wavfiles, or verify your module-levelbuild.gradledoesn't have any rules that exclude assets.
Path参数错误
Ensure theinputPathyou're passing toWaveTools.wavread()is exactly"abaa.wav"—do not add a leading slash (like"/abaa.wav"). TheAssetManager.open()method expects paths relative to the assets root, so leading slashes will cause it to look for a non-existent directory.Android Studio缓存/同步问题
Sometimes the IDE fails to sync assets properly. Try these steps:- Go to Build > Clean Project
- Then Build > Rebuild Project
- If that doesn't work, go to File > Invalidate Caches... > Invalidate and Restart
Bonus note: I noticed a logical error in your WaveTools code at this line:
if (mySampleRate > mySampleRate){
This will always evaluate to false (a number can't be greater than itself). You probably meant to check if the sample rate isn't 8kHz, like:
if (mySampleRate != 8000){
This won't fix the FileNotFoundException, but it's a bug you'll want to address later.
内容的提问来源于stack exchange,提问作者Mohamad Handy Nugraha

