android图片上传

时间:2010-09-27 06:23:01

标签: android

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

2 个答案:

答案 0 :(得分:5)

我为你构建了这些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;
}

到目前为止应该工作。在您的网络服务器上,您需要一些PHP脚本,它会为成功上传返回“1”,而对于错误则返回其他内容。我还建议在ASyncTask中执行此操作,以防止在上载期间阻止用户。 在网络服务器端,你有一个名为“uploadedfile”的文件。希望有所帮助!

答案 1 :(得分:0)

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

POST / HTTP / 1.1
主持人:jmaster
User-Agent:Mozilla / 5.0(Windows; U; Windows NT 5.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
Accept-Language:pl,en-us; q = 0.7,en; q = 0.3
Accept-Encoding:gzip,deflate
Accept-Charset:ISO-8859-2,utf-8; q = 0.7,*; q = 0.7
推荐人:http://shop/index.php/index/register/b/
内容类型:multipart / form-data;边界= --------------------------- 19187836022413
X-Forwarded-For:127.0.0.1
X-Forwarded-Host:jmaster
X-Forwarded-Server:jmaster
连接:保持活力
内容长度:38682
----------------------------- 19187836022413
内容处理:表格数据; NAME = “file2的”;文件名= “Clipboard02.png” 内容类型:image / png
‰PNG

......这就是它的方式。
----------------------------- 19187836022413
你正在结束传播 ---------------------------- 19187836022413

希望这会有所帮助。

相关问题