无法将图像从Android摄像头上传到.net服务器

时间:2017-05-24 06:55:45

标签: android image image-uploading socket-timeout-exception

我在我的应用中使用.net Web服务。我必须上传图像并将其放在服务器上,但每次我都得到socketTimeOutException。

//上传图片的代码

private void onCaptureImageResult(Intent data) {
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".jpg");

    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    ivImage.setImageBitmap(thumbnail);

    imageFlag = true;
    //get the current timeStamp and strore that in the time Variable
    Long tsLong = System.currentTimeMillis() / 1000;
    timestamp = tsLong.toString();


    Bitmap image1 = ((BitmapDrawable) ivImage.getDrawable()).getBitmap();


    new PaymentSlips(image1).execute();

}

// asynk任务的代码

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

    ProgressDialog progressDialog;
    String ResposeFromPaymentRequestApi;


    private Bitmap userfile;

    public PaymentSlips(Bitmap image) {
        this.userfile = image;

    }



    @Override
    protected Void doInBackground(String... params) {


        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //compress the image to jpg format
        if (!(userfile == null))
            userfile.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);


        /*
        * encode image to base64 so that it can be picked by saveImage.php file
        * */

        String encodeImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);

        //generate hashMap to store encodedImage and the name
        HashMap<String, String> detail = new HashMap<>();
        detail.put("image", encodeImage);
        try {

            String dataToSend = hashMapToUrl(detail);
            WebService wsc = new WebService();
            ResposeFromPaymentRequestApi = wsc.PaymentSlips("1", dataToSend, "reqSlip", serviceToken, "PaymentSlips");
            return null;

            /*String response = Request.post(new Config().updateProfilePic, dataToSend);

            //return the response
            return response;*/

        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }

    }

    @Override
    protected void onPostExecute(Void result) {

        //Set response
        try {
            Log.e("TAG", "getimageupload" + ResposeFromPaymentRequestApi);


        } catch (Exception e) {
            e.printStackTrace();
        }

        progressDialog.dismiss();
    }


    @Override
    protected void onPreExecute() {
        //Make ProgressBar invisible
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Please wait.......");
        progressDialog.show();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }

}

private String hashMapToUrl(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for (Map.Entry<String, String> entry : params.entrySet()) {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}

// .net web service的代码

public static String PaymentSlips(String installmentid,String image,String slipStatus,String serviceToken, String webMethName) {
    String resTxt = null;

    SoapObject request = new SoapObject(NAMESPACE, webMethName);

    PropertyInfo loginUserPI = new PropertyInfo();


    // Adding firstname
    loginUserPI.setName("installmentid");
    loginUserPI.setValue(installmentid);
    loginUserPI.setType(String.class);
    request.addProperty(loginUserPI);
    request.addProperty("image",""+image);
    request.addProperty("slipStatus",""+slipStatus);
    request.addProperty("serviceToken", "" + serviceToken);


    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);

    envelope.dotNet = true;

    envelope.setOutputSoapObject(request);

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {

        androidHttpTransport.call(SOAP_ACTION + webMethName, envelope);

        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

        resTxt = response.toString();

    } catch (Exception e) {

        e.printStackTrace();

        resTxt = "Error occured";
    }

    return resTxt;

}

0 个答案:

没有答案