Android 安卓图像上传

Android 安卓图像上传,android,Android,你好,我是android开发的新手 我想知道如何在android中上传图像 我没有找到任何有用的教程 你能给我一些指导吗,请帮帮我。我为你建立了这个lil方法: private boolean handlePicture(String filePath, String mimeType) { HttpURLConnection connection = null; DataOutputStream outStream = null; DataInputStr

你好,我是android开发的新手 我想知道如何在android中上传图像 我没有找到任何有用的教程
你能给我一些指导吗,请帮帮我。

我为你建立了这个lil方法:

private boolean handlePicture(String filePath, String mimeType) {       
    HttpURLConnection connection = null;
    DataOutputStream outStream = null;
    DataInputStream inStream = null;

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

    int bytesRead, bytesAvailable, bufferSize;

    byte[] buffer;

    int maxBufferSize = 1*1024*1024;

    String urlString = "http://www.yourwebserver.com/youruploadscript.php";

    try {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(new File(filePath));
        } catch(FileNotFoundException e) { }
        URL url = new URL(urlString);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);            

        outStream = new DataOutputStream(connection.getOutputStream());

        outStream.writeBytes(addParam("someparam", "content of some param", twoHyphens, boundary, lineEnd));                

        outStream.writeBytes(twoHyphens + boundary + lineEnd);
        outStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + filePath +"\"" + lineEnd + "Content-Type: " + mimeType + lineEnd + "Content-Transfer-Encoding: binary" + lineEnd);          
        outStream.writeBytes(lineEnd);

        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

          while (bytesRead > 0) {
              outStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

          outStream.writeBytes(lineEnd);
          outStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        fileInputStream.close();
        outStream.flush();
        outStream.close();  
    } catch (MalformedURLException e) {
        Log.e("DEBUG", "[MalformedURLException while sending a picture]");
    } catch (IOException e) {
        Log.e("DEBUG", "[IOException while sending a picture]"); 
    }

    try {
           inStream = new DataInputStream( connection.getInputStream() );
           String str;

           while (( str = inStream.readLine()) != null) {
               if(str=="1") {
                   return true;
               } else {
                   return false;
               }
           }
           inStream.close();
      } catch (IOException e){
          Log.e("DEBUG", "[IOException while sending a picture and receiving the response]");
      }
    return false;
}

private String addParam(String key, String value, String twoHyphens, String boundary, String lineEnd) {
        return twoHyphens + boundary + lineEnd + "Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd + lineEnd + value + lineEnd;
}
到目前为止应该是有效的。在您的Web服务器上,您需要一些PHP脚本,该脚本返回1表示成功上载,返回其他内容表示错误。我还建议在ASyncTask中执行此操作,以防止在上载过程中阻塞用户。
在Web服务器端,有一个名为uploadedfile的文件。希望有帮助

我没有这方面的教程。这里有一个例子:np

POST/HTTP/1.1 主持人:jmaster 用户代理:Mozilla/5.0 Windows;Uwindowsnt5.1;pl;rv:1.9.2.10 Gecko/20100914 Firefox/3.6.10 接受:text/html、application/xhtml+xml、application/xml;q=0.9,/;q=0.8 接受语言:pl,en us;q=0.7,en;q=0.3 接受编码:gzip,deflate 接受字符集:ISO-8859-2,utf-8;q=0.7,*;q=0.7 推荐人: 内容类型:多部分/表单数据;边界=-------------19187836022413 X-For:127.0.0.1 X-转发主机:jmaster X-Forwarded-Server:jmaster 连接:保持活力 内容长度:38682 ---------------19187836022413 内容配置:表单数据;name=file2;filename=Clipboard02.png 内容类型:图像/png ‰巴布亚新几内亚 ? ... 事情就是这样的。 ---------------19187836022413 你正在停止传输。 --------------19187836022413


希望这能有所帮助。

您能更具体地说明如何处理此图像吗?有很多方法可以处理图像,我需要选择一种最适合您需要的方法。有了best,你所想要的就是。你会在阅读完这个答案后更新你的答案吗?我正试图上传到应用程序引擎,在修复之后。。成功了!您好,我今天试图使用这段代码,但是行outStream=newdataoutputstreamconnection.getOutputStream;and inStream=new DataInputStream connection.getInputStream;正在抛出异常。我不知道为什么!你能帮忙吗?这是什么类型的IO异常?你在哪里使用代码?模拟器还是手机,有没有SD卡?连接正常吗?为什么。。。如果你不介意的话,我可以得到php函数吗。。。thnx: