Android 正在从服务器下载文件并将其保存到本地存储,但无法使用该文件
我正在从服务器下载音频或视频或.png或pdf文件。例如,我正在从服务器下载音频文件,但无法播放该文件,我认为该文件已编码。该文件存储在DropBox中。作为回应,这是000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\ 我可以下载它,但无法播放该文件。实际上,文件大小为434Kb,但显示为2.3MB 这是密码Android 正在从服务器下载文件并将其保存到本地存储,但无法使用该文件,android,retrofit,retrofit2,Android,Retrofit,Retrofit2,我正在从服务器下载音频或视频或.png或pdf文件。例如,我正在从服务器下载音频文件,但无法播放该文件,我认为该文件已编码。该文件存储在DropBox中。作为回应,这是000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\ 我可以下载它,但无法播放该文件。实际上,文件大小为434Kb,但显示为2.3MB 这是密码 ApiClient apiClient = new ApiClient();
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();
}
ApiClient-ApiClient=new-ApiClient();
RestClient restClientAPI=apiClient.getClient();
Call Call=restcliententapi.downloadfilewithfixedrl(listStudentRes.get(fragmentListner.getAppPreferenceHelper().GetOptitlePosition()).getOrgId()、datumArrayList.get(Integer.parseInt(view.getTag().toString()).getFileName());
call.enqueue(新的2.Callback()
{
@SuppressLint(“StaticFieldLeak”)
@凌驾
公共void onResponse(调用、响应){
Log.d(“Response:”,“+Response.body());
Log.d(“responseContentLength:”,“+response.body().contentLength());
String directoryName=“ValaiSchool Audio”;
文件myDir;
如果(!directoryName.equals(“”){
myDir=新文件(ctx.getExternalFilesDir(ctx.getString(R.string.app_name)),目录名);
}否则{
myDir=新文件(ctx.getExternalFilesDir(ctx.getString(R.string.app_name)),null);
}
如果(!myDir.exists()){
myDir.mkdirs();
}
File outputFile=新文件(myDir,datumArrayList.get(Integer.parseInt(view.getTag().toString()).getFileName());
Log.d(“outputFile”,即“+outputFile”);
试一试{
FileOutputStream fileOutput=新的FileOutputStream(outputFile);
InputStream InputStream=response.body().ByTestStream();
Log.d(“inputStream”,即“+inputStream”);
totalSize=response.body().contentLength();
字节[]缓冲区=新字节[1024*1024];
int缓冲长度;
长总计=0;
而((bufferLength=inputStream.read(buffer))>0){
总长度+=缓冲区长度;
fileOutput.write(buffer,0,bufferLength);
Log.d(“下载”和“+((int)((total*100)/totalSize));
}
fileOutput.flush();
if(fileOutput!=null){
fileOutput.close();
}
如果(inputStream!=null){
inputStream.close();
}
}捕获(最终格式错误){
e、 printStackTrace();
Log.e(“MalformedURLException”,“MalformedURLException>>”+e.getMessage());
}捕获(协议例外e){
e、 printStackTrace();
Log.e(“ProtocolException”,“ProtocolException>>”+e.getMessage());
}捕获(IOE异常){
e、 printStackTrace();
Log.e(“IOException”,“IOException>>”+e.getMessage());
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.d(“错误”和“+t”);
}
});
}否则{
circularFileDownload.showMessage();
}
改装接口:
@Streaming
@GET("./Dropbox/fileDownload")
Call<ResponseBody> downloadFileWithFixedUrl(@Header("org_id") Integer org_id ,@Header("File_Name") String File_Name);
@流媒体
@获取(“./Dropbox/fileDownload”)
调用downloadFileWithFixedUrl(@Header(“org\u id”)整数org\u id,@Header(“File\u Name”)字符串File\u Name);
您的文件名是否也包含文件扩展名类型?否。文件扩展名是什么?如.mp3或.mp4?正如您所说,您可以图像、视频、音频任何内容,因此您也应该具有适当的文件扩展名,如png用于图像,mp4用于音频等。它包含。我可以下载但无法播放它