2015-04-12 71 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. 視頻的並不總是工作,這取決於編解碼器