Android的图片上传

Android的图片上传

本文介绍了Android的图片上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是在新的Andr​​oid开发刍议    我想知道怎么上传图片的机器人    我不发现任何有用的教程本    ü可以给我一些指导,请,帮助我。

hello i am new in android devlopment i want to know how to upload an image in android i dont found any usefull tutorial for this can u give me some instruction,pls,help me out.

推荐答案

我建立了这个可爱的小方法,您:

I built this lil methods for you:

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的,以prevent阻止上传过程中用户。在Web服务器方面,你已经得到了名为UploadedFile的文件。希望帮助!

Should work so far. On your webserver you need some PHP Script which returns a "1" for a successful upload and something else for an error. I also suggest to do this in a ASyncTask, to prevent blocking the user during the uploading.On the webserver side you've got a file in the name "uploadedfile". Hope that helps!

这篇关于Android的图片上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:54