为用户电报漫游器发送照片时出现问题

时间:2019-02-01 10:49:30

标签: java android

向用户发送照片的问题

  

02-01 18:47:37.682 5209-5209 /? E / libprocessgroup:制作失败并   chown / acct / uid_10067:只读文件系统02-01 18:47:38.042   5209-5209 / com.example.myapplication E / imagePath:   /storage/emulated/0/Pictures/s.jpg 02-01 18:47:38.042   5209-5209 / com.example.myapplication E /开始:上传02-01 18:47:39.329   5209-5242 / com.example.myapplication E / EGL_adreno:CreateContext   rcMajorVersion:3,minorVersion:1 02-01 18:47:39.410   5209-5242 / com.example.myapplication E / EGL_adreno:tid 5242:   eglSurfaceAttrib(1316):错误0x3009(EGL_BAD_MATCH)

    public void uploadFile(String sFU) {
    String imagePath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/"+"s.jpg";
    Log.e("imagePath", imagePath);
    Log.e("start", "upload");

    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String lineEnd = "\r\n";
    String boundary = "*****";
    String twoHyphens = "--";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File sourceFile = new File(imagePath);


    if (!sourceFile.isFile()) {

        Log.e("uploadFile", "Source File not exist :"+imagePath);


    }
    else
    {
        try {

            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            URL url = new URL("https://api.telegram.org/bot"+token+"/sendPhoto?chat_id="+"651287199");

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("ENCTYPE", "multipart/form-data");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            conn.setRequestProperty("uploaded_file", imagePath);

            dos = new DataOutputStream(conn.getOutputStream());

            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                    + imagePath + "\"" + lineEnd);

            dos.writeBytes(lineEnd);


            bytesAvailable = fileInputStream.available();

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


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

            while (bytesRead > 0) {

                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            }


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

             int sRC = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();

            Log.i("uploadFile", "HTTP Response is : "+ serverResponseMessage + ": " + sRC);



            fileInputStream.close();
            dos.flush();
            dos.close();
            File f2 = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/.browser" );


        } catch (MalformedURLException ex) {


            ex.printStackTrace();

            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
        } catch (Exception e) {


            e.printStackTrace();


        }




    }
}

0 个答案:

没有答案