hash
hash

Reputation: 5406

after uploading PNG file to server not working

I have this problem.before upload PNG file to server im converting it to byte array like this

Bitmap bitmap = BitmapFactory.decodeFile(signature_path);          
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);

Then add my Image to POST request like this

JSONArray nameValueArray = new JSONArray();
signature_path_value.put("name", "uploadfile");
signature_path_value.put("value", image_str);
nameValueArray.put(signature_path_value);

data.put(NAME_VALUE_LIST, nameValueArray);

String restData = org.json.simple.JSONValue.toJSONString(data);

HttpClient httpClient = new DefaultHttpClient();
HttpPost req = new HttpPost(rest_url);
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(METHOD, SET_ENTRY));
nameValuePairs.add(new BasicNameValuePair(INPUT_TYPE, JSON));
nameValuePairs.add(new BasicNameValuePair(RESPONSE_TYPE, JSON));
nameValuePairs.add(new BasicNameValuePair(REST_DATA, restData));
req.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Send POST request
httpClient.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
HttpResponse res = httpClient.execute(req);

But in my site image Look like this

iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABHNCSVQICAgIfAhkiAAAEIFJREFUaIG9mXuQ3FWVxz/n/h79mJ5JmDxIgsGAETFRggq+WDAp8MXiKoszy4qsItZS6OJjV2V9QcddnxRrIZTrWoII5QLTxaKr4sK6MCUoGIUYMAkSEpIAYZIJmcyju3+Pe+/ZP37dM4kJmmRXb9WtX03P7f6d7znf7znn3iv8H8YjzUvOHt2zZ92

Why is this happen?why it didnt upload like img.png

can anyone help me..

update---

logcat

04-20 12:28:22.535: E/AndroidRuntime(18823): FATAL EXCEPTION: main
04-20 12:28:22.535: E/AndroidRuntime(18823): Process: com.stonewheeltest, PID: 18823
04-20 12:28:22.535: E/AndroidRuntime(18823): java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
04-20 12:28:22.535: E/AndroidRuntime(18823):    at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:85)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at newCrm.Sugercrm_sync.uploadFile(Sugercrm_sync.java:207)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at newCrm.Sugercrm_sync.access$0(Sugercrm_sync.java:192)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at newCrm.Sugercrm_sync$AddNewRecord.onPostExecute(Sugercrm_sync.java:185)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at newCrm.Sugercrm_sync$AddNewRecord.onPostExecute(Sugercrm_sync.java:1)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at android.os.AsyncTask.finish(AsyncTask.java:632)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at android.os.Looper.loop(Looper.java:146)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at android.app.ActivityThread.main(ActivityThread.java:5653)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at java.lang.reflect.Method.invokeNative(Native Method)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at java.lang.reflect.Method.invoke(Method.java:515)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 391

Answers (1)

Ravi
Ravi

Reputation: 35549

refer this link to upload image without using multipart http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106

and with using multipart entity use this link. http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/

you can use

reqEntity.addPart("uploadfile", new FileBody(<File object of your image path>, "image/jpeg,image/png"));

Upvotes: 1

Related Questions