2015-04-12 2 views
0

У меня есть следующая проблема.Android httpresponse загрузить png из index.php

  1. пришлю httpost в файл index.php
  2. Мой файл index.php возвращает заголовок («изображение/PNG») и, конечно, файл
  3. Тогда мой код будет загрузить ответ в файл на устройство SD Card
  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

Когда я меняю сообщение, чтобы получить прямую ссылку на изображение, он будет загружен. Поэтому я думаю, что проблема лежит в заголовках файла php. HttpClient httpclient = новый MyHttpClient (mContext); HttpGet httppost = новый HttpGet ("http://example.com/General-information-Example.jpg"); – user3465299

+0

Я проверил одинаковые проблемы с файлами> 25 МБ. Когда я получаю файл размером более 25 МБ, он также не открывается :(. – user3465299

ответ

0

поставил output.flush(); быть переднего закрытие вашего выхода

ваши наконец заявление должно быть что-то вроде этого: if (output != null) try {output.flush(); output.close(); } catch (IOException logOrIgnore) {}

+0

Спасибо, но это не решило проблему. Оно по-прежнему загружается, но я не могу открыть изображение. – user3465299

0
  1. Изображение, где CMYK, должна быть RGB
  2. Video, не всегда работаем, в зависимости от кодека
Смежные вопросы