图像没有发布android

时间:2014-01-18 11:23:42

标签: java android image

不确定这里发生了什么,没有出现任何红色错误,但图片的帖子永远不会发生,它没有触及我的服务器......

以下是橙色错误:

01-18 06:36:48.372: W/InputEventReceiver(7867): Attempted to finish an input event but the input event receiver has already been disposed.
01-18 06:36:48.372: W/InputEventReceiver(7867): Attempted to finish an input event but the input event receiver has already been disposed.
01-18 06:36:48.372: I/Choreographer(7867): Skipped 87 frames!  The application may be doing too much work on its main thread.
01-18 06:36:48.372: W/ViewRootImpl(7867): Dropping event due to root view being removed: MotionEvent { action=ACTION_UP, id[0]=0, x[0]=287.0, y[0]=-358.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=5442878, downTime=5442722, deviceId=0, source=0x1002 }
01-18 06:37:31.392: I/uploadFile(7867): HTTP Response is : Internal Server Error: 500
01-18 06:37:31.522: E/Async Time(7867): Async entered POSTTTT

以下是代码:

private class ImageUploadTask extends AsyncTask<String, Void, String> {

        private String webAddressToPost = "http://10.0.2.2:3000/wardrobe";

        // File sourceFile = new File(imageview);
        String fileName = "/sdcard/IMG_2016.JPG";
        File sourceFile = new File(fileName );

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

        // private ProgressDialog dialog
        private ProgressDialog progressDialog = new ProgressDialog(
                wardrobe.this);

        @Override
        protected void onPreExecute() {
            Log.e("Async Time", "Async entered 1 PREEE");
            progressDialog.setMessage("Uploading...");
            progressDialog.show();
        }

        @Override
        protected String doInBackground(String... arg0) {
            Log.e("Async Time", "Async entered DURINGG");
            try {
                // open a URL connection to the Servlet
                FileInputStream fileInputStream = new FileInputStream(
                        sourceFile);
                URL url = new URL(webAddressToPost);

                // Open a HTTP connection to the URL
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true); // Allow Inputs
                conn.setDoOutput(true); // Allow Outputs
                conn.setUseCaches(false); // Don't use a Cached Copy
                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", fileName);

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

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

                dos.writeBytes(lineEnd);

                // create a buffer of maximum size
                bytesAvailable = fileInputStream.available();

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

                // read file and write it into form...
                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);

                }

                // send multipart form data necesssary after file data...
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                // Responses from the server (code and message)
                serverResponseCode = conn.getResponseCode();
                String serverResponseMessage = conn.getResponseMessage();

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

                if (serverResponseCode == 200) {

                    String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
                            + " F:/wamp/wamp/www/uploads";
                    imageTextSelect.setText(msg);
                    Toast.makeText(wardrobe.this, "File Upload Complete.",
                            Toast.LENGTH_SHORT).show();

                }

                // close the streams //
                fileInputStream.close();
                dos.flush();
                dos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            Log.e("Async Time", "Async entered POSTTTT");
            progressDialog.dismiss();
            Toast.makeText(getApplicationContext(), "file uploaded",
                    Toast.LENGTH_LONG).show();
        }
    }

1 个答案:

答案 0 :(得分:0)

您直接作为 sdcard String fileName = "/sdcard/IMG_2016.JPG";提供的信息可能就是原因。

尝试以下代码..

String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                            "/IMG_2016.JPG";