Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I solve the following exception when uploading an image to server path?
    text
    copied!<p>I am working on Android and I am facing problem when uploading image to the server from my phone. I am using multipartentity using post method.</p> <p>I am using the following library <strong>httpmime-4.3-beta1.jar</strong>.</p> <p>I am getting the following exception</p> <pre><code>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.&lt;init&gt;(ByteArrayBody.java:68) 04-24 14:47:43.815: E/AndroidRuntime(29526): at org.apache.http.entity.mime.content.ByteArrayBody.&lt;init&gt;(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) </code></pre> <p>04-24 14:47:43.815: E/AndroidRuntime(29526): at dalvik.system.NativeStart.main(Native Method)</p> <p>And my code is as follows.</p> <pre><code>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 &lt; REQUIRED_SIZE || height_tmp / 2 &lt; 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()); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload