2015-04-12 69 views
0

我遇到以下问题。Android httpresponse从index.php下载png

  1. 我会发一个httpost到index.php文件
  2. 我的index.php文件,返回头(“图像/ PNG”)和关闭过程中的文件
  3. 然后我的代码会下载响应对设备的SD卡
  4. 它被下载并使用正确的器件尺寸SD卡上的文件(这个图像中的例子是,34MB)

但是,当它已经被下载了,我不能打开设备上的文件。 有人知道问题可以解决吗?

String file = "18228"; 
 

 
HttpClient httpclient=new MyHttpClient(mContext); // MyHttpCient handles https 
 
HttpPost httppost= new HttpPost("https://example.com/json/index.php"); // make sure the url is correct. 
 

 
HttpResponse response=httpclient.execute(httppost); 
 
nameValuePairs.add(new BasicNameValuePair("requestFile",file)); 
 

 
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
 
StrictMode.setThreadPolicy(policy); 
 
File SDCardRoot = Environment.getExternalStorageDirectory(); 
 
File fileDownload = new File(SDCardRoot + "/MyFiles" , "General-information-Example.jpg"); 
 

 
InputStream input = null; 
 
OutputStream output = null; 
 
byte[] buffer = new byte[1024]; 
 

 
try { 
 
    System.out.println("Downloading file..."); 
 
    input = response.getEntity().getContent(); 
 
    output = new FileOutputStream(fileDownload); 
 
    for (int length; (length = input.read(buffer)) > 0;) { 
 
    output.write(buffer, 0, length); 
 
    } 
 
    System.out.println("File successfully downloaded!"); 
 
} finally { 
 
    if (output != null) try { output.close(); } catch (IOException logOrIgnore) {} 
 
    if (input != null) try { input.close(); } catch (IOException logOrIgnore) {} 
 
}

<?php 
 

 
header('Content-Type: '.$loadFile['header'].''); \t 
 
header('Content-Length: ' . filesize($loadFile['file'])); 
 
header('Pragma: public'); 
 
header('Accept-Ranges: bytes'); 
 
header('Cache-Control: no-cache'); 
 
header('Content-Disposition: filename="' . basename($loadFile['file'])); 
 
header('X-Pad: avoid browser bug'); 
 
readfile($loadFile['file']); 
 
exit; 
 

 
?>

04-12 12:52:35.080 4822-4999/info.androidhive.customlistviewvolley I/System.out﹕ Downloading file... 
 
04-12 12:52:47.410 4822-4999/info.androidhive.customlistviewvolley I/System.out﹕ File successfully downloaded! 
 
04-12 12:53:08.370 4822-4822/info.androidhive.customlistviewvolley D/OpenGLRenderer﹕ Flushing caches (mode 0) 
 
04-12 12:53:08.400 4822-4822/info.androidhive.customlistviewvolley D/OpenGLRenderer﹕ Flushing caches (mode 1)

+0

同问题符合Pdf,Mp4和其他文件时,我下载到设备。我无法打开文件。 – user3465299

+0

当我通过直接图片链接将帖子更改为get时,它会下载。所以我认为这个问题与PHP文件的标题一致。 HttpClient httpclient = new MyHttpClient(mContext); HttpGet httppost = new HttpGet(“http://example.com/General-information-Example.jpg”); – user3465299

+0

我查了同样的问题符合大于25MB的文件。当我做一个大于25MB的文件时,它也不会打开:(。 – user3465299

回答

0

output.flush();是前关闭你的输出

你最后的说法应该是这样的:if (output != null) try {output.flush(); output.close(); } catch (IOException logOrIgnore) {}

+0

谢谢,但这没有解决问题。它仍然下载,但我无法打开图像。 – user3465299

0
  1. 图片,其中CMYK,应该是RGB
  2. 视频的并不总是工作,这取决于编解码器