android中的大尺寸图片上传和阅读问题?

时间:2010-11-12 10:55:32

标签: android

任何人都会指导我,如果我从Android手机上学习图像 我的应用程序适用于小图像,但如果我的相机是3或5百万像素,它需要

大尺寸图片,例如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 个答案:

答案 0 :(得分:0)

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