将图像上载到服务器路径时如何解决以下异常?

时间:2013-04-24 08:59:32

标签: android image-uploading multipartentity

我正在Android上运行,当我从手机上传图片到服务器时,我遇到了问题。我使用post方法使用multipartentity。

我正在使用以下库 httpmime-4.3-beta1.jar

我收到以下异常

04-24 14:47:43.815: E/AndroidRuntime(29526): FATAL EXCEPTION: main
04-24 14:47:43.815: E/AndroidRuntime(29526): java.lang.NoClassDefFoundError:  org.apache.http.entity.ContentType
04-24 14:47:43.815: E/AndroidRuntime(29526):    at org.apache.http.entity.mime.content.ByteArrayBody.<init>(ByteArrayBody.java:68)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at org.apache.http.entity.mime.content.ByteArrayBody.<init>(ByteArrayBody.java:88)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at com.example.uploadingimagetoserver.MainActivity.executeMultipartPost(MainActivity.java:161)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at com.example.uploadingimagetoserver.MainActivity.onActivityResult(MainActivity.java:71)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at android.app.Activity.dispatchActivityResult(Activity.java:5390)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at android.app.ActivityThread.access$1200(ActivityThread.java:140)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at android.os.Looper.loop(Looper.java:137)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at android.app.ActivityThread.main(ActivityThread.java:4921)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at java.lang.reflect.Method.invokeNative(Native Method)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at java.lang.reflect.Method.invoke(Method.java:511)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
04-24 14:47:43.815: E/AndroidRuntime(29526):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

04-24 14:47:43.815:E / AndroidRuntime(29526):at dalvik.system.NativeStart.main(Native Method)

我的代码如下。

public class MainActivity extends Activity {

private Bitmap bm;
private final int CAMERA_PICTURE = 1;
private final int GALLERY_PICTURE = 2;
private ImageView userPictureImageView;
private Intent pictureActionIntent = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    userPictureImageView = (ImageView) findViewById(R.id.imageView1);
}
public void uploadImage(View v){

     startDialog();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GALLERY_PICTURE) {
        Uri uri = data.getData();
        if (uri != null) {
            // User had pick an image.
            Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
            cursor.moveToFirst();
            // Link to the image
            final String imageFilePath = cursor.getString(0);
            File photos = new File(imageFilePath);
            bm = BitmapFactory.decodeFile(photos.getAbsolutePath());
            try {
                executeMultipartPost();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Bitmap b = decodeFile(photos);
            b = Bitmap.createScaledBitmap(b, 150, 150, true);
            userPictureImageView.setImageBitmap(b);
            cursor.close();
        }
        else {
            Toast toast = Toast.makeText(this, "No Image is selected.", Toast.LENGTH_LONG);
            toast.show();
        }
    }
    else if (requestCode == CAMERA_PICTURE) {
        if (data.getExtras() != null) {
            // here is the image from camera
            Bitmap bitmap = (Bitmap) data.getExtras().get("data");
            bm = bitmap;
            try {
                executeMultipartPost();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            userPictureImageView.setImageBitmap(bitmap);
        }
    }
}

private Bitmap decodeFile(File f) {
    try {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 70;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale++;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    }
    catch (FileNotFoundException e) {
    }
    return null;
}

private void startDialog() {
    AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
    myAlertDialog.setTitle("Upload Pictures Option");
    myAlertDialog.setMessage("How do you want to set your picture?");

    myAlertDialog.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT, null);
            pictureActionIntent.setType("image/*");
            pictureActionIntent.putExtra("return-data", true);
            startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
        }
    });

    myAlertDialog.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(pictureActionIntent, CAMERA_PICTURE);
        }
    });
    myAlertDialog.show();
}

 public void executeMultipartPost() throws Exception {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bm.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost("http://www.uspdhub.com/Upload");
            ByteArrayBody bab = new ByteArrayBody(data, "imageSample.jpg");

            // File file= new File("/mnt/sdcard/forest.png");
            // FileBody bin = new FileBody(file);
            MultipartEntity reqEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("uploaded", bab);
            reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
            postRequest.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();

            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
            System.out.println("Response: " + s);


        } catch (Exception e) {
            // handle exception here
            Log.e(e.getClass().getName(), e.getMessage());
        }
    }


}

1 个答案:

答案 0 :(得分:2)

我不确定这是否会有所帮助,但在这里。 您是否还添加了httpcore-4.3-beta1.jar,httpclient-4.3-beta1.jar和apache-mime4j? 我的理解是,如果您正在使用MultipartEntity,则需要所有这些。