2010-11-12 51 views
0

任何一個指導我,如果我上傳從Android手機的Image Pro語法 我的應用程序工作正常,小圖像,但如果我的相機是3或500萬像素的情況下,它需要大尺寸圖片上傳和閱讀在android的問題?

大尺寸的圖像,例如2mb,需要花費太多時間上傳,有時會讓我感到內存異常。

任何人建議我如何減少圖像大小,使圖像質量不打擾 或任何其他建議來解決這個問題?

public class HttpRequest { 


    public static HttpData post(String sUrl, Hashtable<String, String> params, ArrayList<File> files,Context mycontext,String path) { 
     HttpData ret = new HttpData(); 
     try { 
       String boundary = "*****************************************"; 
       String newLine = "\r\n"; 
       int bytesAvailable; 
       int bufferSize; 
       int maxBufferSize = 4096; 
       int bytesRead; 

       URL url = new URL(sUrl); 


       HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
       con.setDoInput(true); 
       con.setDoOutput(true); 
       con.setUseCaches(false); 
       con.setRequestMethod("POST"); 
       con.setRequestProperty("Connection", "Keep-Alive"); 
       con.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 
       DataOutputStream dos = new DataOutputStream(con.getOutputStream()); 

       //dos.writeChars(params); 

       //upload files 
       for (int i=0; i<files.size(); i++) { 
         Log.i("HREQ", i+""); 
         //FileInputStream fis = new FileInputStream(files.get(i)); 
         InputStream fis=mycontext.getContentResolver().openInputStream(Uri.parse(path)); 
         dos.writeBytes("--" + boundary + newLine); 
         dos.writeBytes("Content-Disposition: form-data; name=myfile;filename=" 
         + "test.png" + newLine + newLine); 
         bytesAvailable = fis.available(); 
         bufferSize = Math.min(bytesAvailable, maxBufferSize); 
         byte[] buffer = new byte[bufferSize]; 
         bytesRead = fis.read(buffer, 0, bufferSize); 
         while (bytesRead > 0) { 
           dos.write(buffer, 0, bufferSize); 
           bytesAvailable = fis.available(); 
           bufferSize = Math.min(bytesAvailable, maxBufferSize); 
           bytesRead = fis.read(buffer, 0, bufferSize); 
         } 
         dos.writeBytes(newLine); 
         dos.writeBytes("--" + boundary + "--" + newLine); 
         fis.close(); 
       } 
       // Now write the data 

       Enumeration keys = params.keys(); 
       String key, val; 
       while (keys.hasMoreElements()) { 
         key = keys.nextElement().toString(); 
         val = params.get(key); 
         dos.writeBytes("--" + boundary + newLine); 
         dos.writeBytes("Content-Disposition: form-data;name=" 
         + key + newLine + newLine + val); 
         dos.writeBytes(newLine); 
         dos.writeBytes("--" + boundary + "--" + newLine); 

       } 
       dos.flush(); 

       BufferedReader rd = new BufferedReader(
       new InputStreamReader(con.getInputStream())); 
       String line; 
       while ((line = rd.readLine()) != null) { 
         ret.content += line + "\r\n"; 
       } 

       /* 
       //get headers 
       Map<String, List<String>> headers = con.getHeaderFields(); 
       Set<Entry<String, List<String>>> hKeys = headers.entrySet(); 
       for (Iterator<Entry<String, List<String>>> i = hKeys.iterator(); i.hasNext();) { 
         Entry<String, List<String>> m = i.next(); 

         Log.w("HEADER_KEY", m.getKey() + ""); 
         ret.headers.put(m.getKey(), m.getValue().toString()); 
         if (m.getKey().equals("set-cookie")) 
         ret.cookies.put(m.getKey(), m.getValue().toString()); 
       } 
       */ 
       dos.close(); 
       rd.close(); 
     } catch (MalformedURLException me) { 
     String value = me.getMessage(); 
     String val = value; 

     } catch (IOException ie) { 
     Log.e("here ", "Exception: "+ie.toString()); 

     } catch (Exception e) { 
       Log.e("HREQ", "Exception: "+e.toString()); 
     } 
     return ret; 
} 

任何幫助,將不勝感激。

+1

上傳圖片的位置和方式? – 2010-11-12 10:59:24

+0

使用http post從圖庫上傳圖像。 – UMAR 2010-11-12 11:45:45

+0

向我們顯示代碼 – 2010-11-12 12:19:36

回答

0

嘗試將其轉換爲位圖,但會降低圖像的質量。