OutOfMemoryError从资产文件夹传递音频文件时出现异常

时间:2013-10-22 11:52:24

标签: android bytearray out-of-memory

代码:

AssetManager am = ctx.getAssets();

        try {

            InputStream is = am.open("kalimba.mp3"); // use recorded file instead of getting file from assets folder.

            int length = is.available();
            Log.v("Length", ""+length);

            audio_data = new byte[length];
            int bytesRead=0;

            ByteArrayOutputStream output = new ByteArrayOutputStream();

            while (bytesRead  != length)

            {
                Log.v("BytesRead", ""+bytesRead);
                Log.v("Audio Data", ""+audio_data);
                output.write(audio_data,bytesRead,length-bytesRead);

            }

          inarry = output.toByteArray();

logcat的:

10-22 07:40:11.516: E/AndroidRuntime(3369): java.lang.OutOfMemoryError
10-22 07:40:11.516: E/AndroidRuntime(3369):     at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
10-22 07:40:11.516: E/AndroidRuntime(3369):     at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201) 
10-22 07:40:11.516: E/AndroidRuntime(3369):     at com.deepak.encydep.MainActivity.getAudioFile(MainActivity.java:149) ----> Error Line
10-22 07:40:11.516: E/AndroidRuntime(3369):     at com.deepak.encydep.MainActivity$1.onClick(MainActivity.java:56)
10-22 07:40:11.516: E/AndroidRuntime(3369):     at android.view.View.performClick(View.java:4240)
10-22 07:40:11.516: E/AndroidRuntime(3369):     at android.view.View$PerformClick.run(View.java:17721)

1 个答案:

答案 0 :(得分:0)

看起来您根本没有更新bytesRead,而且它始终保持为0.这样您的while()永远会跑(在您的情况下 - 直到OutOfMemory)
另外,我没有注意到你从is(你的输入流)读取任何数据到缓冲区(audio_data)