我正在使用 volley 下载文件,但无法打开它,它显示:
无法显示 PDF(name.pdf 格式无效)
还要提到服务返回一个 11kb 字节 [] 和调试,我收到一个 16kb 响应。我正在使用的代码是:
InputStreamVolleyRequest request = new InputStreamVolleyRequest(Request.Method.POST, uri,
new Response.Listener<byte[]>() {
@Override
public void onResponse(byte[] response) {
try {
if (response!=null) {
if(Build.VERSION.SDK_INT>22){
requestPermissions(new String[] {"android.permission.WRITE_EXTERNAL_STORAGE","android.permission.READ_EXTERNAL_STORAGE"}, 1);
}
File baseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
baseDir.mkdirs();
File carpeta = new File(baseDir + nombre_carpeta);
carpeta.mkdirs();
File file = new File(baseDir, documento.NombreFichero.replace("\\","/"));
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(response);
bos.flush();
bos.close();
Log.d("NEWFILE", file.getAbsolutePath());
Toast.makeText(getActivity(), "Descarga completa.", Toast.LENGTH_LONG).show();
} catch (IOException e) { …Run Code Online (Sandbox Code Playgroud)