使用android中的MultipartEntity类上传图片

时间:2015-02-26 06:56:50

标签: android

我必须上传图片文件并将其发送到服务器,所以我使用MultipartEntity类,它给我以下错误信息..

Multiple markers at this line

- MultipartEntity cannot be resolved to a type
- The type new RecoverySystem.ProgressListener(){} must implement the inherited abstract method 
 RecoverySystem.ProgressListener.onProgress(int)
- MultipartEntity cannot be resolved to a type

这是代码

@Override
    protected String doInBackground(Void... params) {
        return uploadFile();
    }

    @SuppressWarnings("deprecation")
    private String uploadFile() {
        String responseString = null;

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://politician.qkzoom.com");
        return responseString;



        MultipartEntity entity = new MultipartEntity(new ProgressListener() {

            public void transferred(long num) {

                publishProgress((int) ((num / (float) totalSize) * 100));
            }
        });

1 个答案:

答案 0 :(得分:0)

您可以通过将图像转换为Base64字符串来上传图像并将字符串发送到服务器然后解码服务器端的Base64字符串以获取图像 此代码将图像转换为Base64字符串

public static final String convertImageToBase64(Bitmap bm) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); // bm is the bitmap
                                                            // object
        byte[] byteArrayImage = baos.toByteArray();

        return Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
    }

这是你不会使用的进口

import java.io.ByteArrayOutputStream;
import android.graphics.Bitmap;
import android.util.Base64;