Android eclipse error on File upload to server

让人想犯罪 __ 提交于 2019-11-30 07:47:10

This is main problem of libs., that all might faced, wrong library causes this issue,

So, following step helpful to you :

(1) Code : Following code is successfully upload photo to the server :

     btnUpload.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (Constant.bitmapPicture != null) {
                StoreByteImage(Constant.bitmapPicture, 90, "my_image");

            } else {
                Toast.makeText(getApplicationContext(), "image null", Toast.LENGTH_LONG).show();
            }
        }
    });

StoreByteImage(...) :

   public boolean StoreByteImage(Bitmap bitmap, int quality, String expName) {

    FileOutputStream fileOutputStream = null;
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File myNewFolder = new File(extStorageDirectory + "/Example");
    if (myNewFolder.mkdirs()) {
        myNewFolder.mkdir();
    }
    try {


        iPath = myNewFolder + "/" + expName + ".jpg";
        fileOutputStream = new FileOutputStream(myNewFolder + "/" + expName + ".jpg");
        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
        bitmap.compress(CompressFormat.JPEG, quality, bos);

        new ImageUploadTask().execute();

        bos.flush();
        bos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return true;
}

Class ImageUploadTask

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

    String sResponse;

    @SuppressWarnings("deprecation")
    @Override
    protected String doInBackground(Void... unsued) {
        try {
            //

            File image = new File(iPath);
            FileBody fileBody = new FileBody(image);

            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(YourImageUploadURLHere);

            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Constant.bitmapPicture.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();

            entity.addPart("ID", new StringBody("1"));
            entity.addPart("pID", new StringBody("1"));
            entity.addPart("userPhoto", fileBody);
            entity.addPart("email", new StringBody("email@gmail.com"));
            entity.addPart("cell", new StringBody("1234567890"));
            entity.addPart("username", new StringBody("name"));

            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost, localContext);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),
                    "UTF-8"));

            sResponse = reader.readLine();
            System.out.println("responce=" + sResponse);

        } catch (Exception e) {

            System.out.println("error=" + e.getMessage());
            return null;
        }
        return "";

    }

    @Override
    protected void onProgressUpdate(Void... unsued) {

    }

    @Override
    protected void onPostExecute(String sResponses) {
        try {

            if (sResponses != null) {

                JSONObject jsonObjSend = new JSONObject(sResponse.toString());

                if (jsonObjSend.getString("status").equals("success")) {

                    // another code
                } else if (jsonObjSend.getString("status").equals("fail")) {

                    // another code
                }

            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "excepttion", Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
        }
    }
}

(2) First Remove All libs/jar file from the your libs Folder :

(3) Find Following libraries/jar file from the internet and put in libs folder :

(4) Add jar from the libraries tab :

(5) Your Order and Export libraries/jar file Tick mark same as following :

Ok, Done. Just these simple steps helpful to you upload image file to server. hope these helphul to you and others.

I have upgraded from 4.3 to 4.4 before i have noticed this error. Could be 4.3 causing it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!