how to Upload image into server in Android?

∥☆過路亽.° 提交于 2019-11-28 09:28:39

I using this:

public class HttpClient extends AsyncTask<Void, Integer, Long> {
    private static final int PROGRESS_DIALOG = 0;

    public ProgressDialog dialog;
    public File file;
    protected Long doInBackground(Void... params) {
        for (File file : files) {

            foto = "/sdcard/CameraExample/" + file.getName();
            DefaultHttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost(urll);



            MultipartEntity mpEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            mpEntity.addPart("form_file", new FileBody(file, "image/jpeg"));

            httppost.setEntity(mpEntity);

            HttpResponse response;
            try {

                response = httpclient.execute(httppost);

                HttpEntity resEntity = response.getEntity();

                if (resEntity != null) {

                }
                if (resEntity != null) {
                    resEntity.consumeContent();
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }



        }
        return null;
    }

    protected void onPostExecute(Long unused) {
        progressDialog.dismiss();

        ((Runnable) ctx ).run();

        super.onPostExecute(unused);
    }

    protected void onPreExecute() {
    progressDialog = new ProgressDialog(ctx);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setMessage("Загрузка фото...");
    progressDialog.setProgress(0);
    progressDialog.setMax(count);
    progressDialog.show();
    }

}

This code using that library:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.james.mime4j.message.Message;

You can find this in Google. If you don't find - i can send you this libraries.

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