从服务器下载文件并将其保存到本地存储,但无法使用该文件

时间:2018-08-13 07:09:54

标签: android retrofit retrofit2

我正在从Server下载音频或视频或.png或pdf文件。示例我从该文件下载音频文件,但无法播放该文件,我认为它已编码。文件存储在DropBox中。作为回应,身体像这样来临000 \ u0000 \ u0000 \ u0000 \ u0000 \ u0000 \ u0000 \ u0000 \ u0000 \ u0000 \ 我可以下载它无法播放该文件。实际上文件大小为434Kb,但显示为2.3MB

这是代码

   ApiClient apiClient = new ApiClient();
                    RestClient restClientAPI = apiClient.getClient();
                    Call<ResponseBody> call = restClientAPI.downloadFileWithFixedUrl(listStudentRes.get(fragmentListner.getAppPreferenceHelper().getTopTitlePosition()).getOrgId(),datumArrayList.get(Integer.parseInt(view.getTag().toString())).getFileName());
                    call.enqueue(new retrofit2.Callback<ResponseBody>()
                    {
                        @SuppressLint("StaticFieldLeak")
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            Log.d("Response : ",""+response.body());
                            Log.d("ResponseContLength : ",""+response.body().contentLength());


                               String directoryName = "ValaiSchool Audio";
                            File myDir;
                            if (!directoryName.equals("")) {
                                myDir = new File(ctx.getExternalFilesDir(ctx.getString(R.string.app_name)), directoryName);
                            } else {
                                myDir = new File(ctx.getExternalFilesDir(ctx.getString(R.string.app_name)), null);
                            }

                            if (!myDir.exists()) {
                                myDir.mkdirs();
                            }
                            File outputFile = new File(myDir, datumArrayList.get(Integer.parseInt(view.getTag().toString())).getFileName());
                            Log.d("outputFile",""+outputFile);
                            try {
                                FileOutputStream fileOutput = new FileOutputStream(outputFile);

                                InputStream inputStream = response.body().byteStream();
                                Log.d("inputStream",""+inputStream);

                                totalSize =response.body().contentLength();

                                byte[] buffer = new byte[1024*1024];

                                int bufferLength;

                                long total = 0;
                                while ((bufferLength = inputStream.read(buffer)) > 0) {
                                    total += bufferLength;
                                    fileOutput.write(buffer, 0, bufferLength);
                                     Log.d("Downloading",""+( (int)((total*100)/totalSize)));

                                }

                                fileOutput.flush();
                                if (fileOutput != null) {
                                    fileOutput.close();
                                }
                                if (inputStream != null) {
                                    inputStream.close();
                                }

                            } catch (final MalformedURLException e) {
                                e.printStackTrace();
                                Log.e("MalformedURLException", "MalformedURLException>>" + e.getMessage());

                            } catch (ProtocolException e) {
                                e.printStackTrace();
                                Log.e("ProtocolException", "ProtocolException>>" + e.getMessage());

                            } catch (IOException e) {
                                e.printStackTrace();
                                Log.e("IOException", "IOException>>" + e.getMessage());

                            }

                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {
                             Log.d("Error",""+t);
                        }
                    });



                } else {
                    circularFileDownload.showMessage();
                }

改造界面:

 @Streaming
@GET("./Dropbox/fileDownload")
Call<ResponseBody> downloadFileWithFixedUrl(@Header("org_id") Integer org_id ,@Header("File_Name") String File_Name);

0 个答案:

没有答案