代码之家  ›  专栏  ›  技术社区  ›  UMAR-MOBITSOLUTIONS

android中的大尺寸图像上传和读取问题?

  •  0
  • UMAR-MOBITSOLUTIONS  · 技术社区  · 15 年前

    我的应用程序可以很好地处理小图像,但如果我的相机是300万像素或500万像素,它就需要

    大尺寸图像,例如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 回复  |  直到 15 年前
        1
  •  0
  •   iamtheexp01    15 年前

    尝试将其转换为位图,但会降低图像质量。