加载字节数组需要太长时间?

时间:2015-02-22 03:30:25

标签: java android

我聘请了某人来编写我的应用。它将从互联网下载的乐器加载到我的应用程序。要下载乐器,速度很快,但在加载乐器时,至少需要一分钟或更长时间。我查看了代码,看看它会减慢速度,但我似乎无法弄明白。任何帮助表示赞赏。

代码:         //文件加载任务

 class SaveInputStreamTask extends AsyncTask<String, Integer, String> {

            private Context context;
            ProgressDialog mProgressDialog;

            public SaveInputStreamTask(Context context) {
                this.context = context;
            }

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();

                // mProgressDialog = new ProgressDialog(context);
                 //mProgressDialog.setMessage("Beat Will Take A Minute To Load When Mixing So Start Recording");
                mProgressDialog = ProgressDialog.show(context, getResources().getString(R.string.app_name), "Beat Will Take Up To A Minute To Load. In The Meantime How's Your Day?");
                 mProgressDialog.show();

            }

            @Override
            protected String doInBackground(String... sUrl) {

                try
                {
                    File file = new File(instrument_file_name);
                    long totalFilesize = file.length();
                    long readSize = 0;
                    FileInputStream fis = new FileInputStream(file);
                    saveInputStream(fis);
                    return "SUCCESS";
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onProgressUpdate(Integer... progress) {
                super.onProgressUpdate(progress);
                // if we get here, length is known, now set indeterminate to false
                mProgressDialog.setIndeterminate(false);
                mProgressDialog.setMax(100);
                mProgressDialog.setProgress(progress[0]);
            }

            @Override
            protected void onPostExecute(String result) {
                mProgressDialog.dismiss();
                if(result == null){
                    Toast.makeText(context, "Loading Beat failed. Please try again", Toast.LENGTH_SHORT).show();
                    RecordRap.this.finish();
                }
                else
                {

                }
            }

            public void saveInputStream(InputStream is) throws IOException
            {
                int n = 0;
                DataInputStream in1;
                in1 = new DataInputStream(is);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();

                try 
                {

                    while ((n = in1.read()) != -1) 
                    {
                        bos.write(n);
                    }
                } 
                catch (IOException e)
                {   
                    e.printStackTrace();
                }

                ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray());
                bb.order(ByteOrder.LITTLE_ENDIAN);
                ShortBuffer sb = bb.asShortBuffer();

                for (int i = 0; i < sb.capacity(); i++) {
                    beatsShortList.add(sb.get(i));
                }
            }
        }

2 个答案:

答案 0 :(得分:1)

所以多亏了@Stephen C问题是while ((n = in1.read()) != -1)所以我添加了一个缓冲区并将代码更改为以下内容并解决了问题,现在加载只需几秒钟。感谢Stephen C的帮助和Ratul Sharker。

更新的代码:

                    byte[] buffer = new byte[0xFFFF];

                    while ((n = in1.read(buffer)) != -1)
                    {
                        bos.write(buffer, 0, n);
                    }

答案 1 :(得分:0)

for (int i = 0; i < sb.capacity(); i++) {
    beatsShortList.add(sb.get(i));
}

这是罪魁祸首你在寻找什么:)