0

is there any method for uploading images from android to php server without using any external library? if available just give me example.

Swapnil
  • 654
  • 7
  • 27
  • If you are uploading small files like images, this could be relevant [Android post Base64 String to PHP](http://stackoverflow.com/questions/9920967/android-post-base64-string-to-php) – James Wong Feb 17 '14 at 06:23
  • @NikolaDespotoski i tried your solution but gives error due to String image_str = Base64.encodeBytes(byte_arr); unavailablity of encodeBytes() function where i will get this library – Swapnil Feb 17 '14 at 06:25

2 Answers2

0

first convert your image into a base 64 byte array encoded string. after that send it to php. extract on server side.Then store that string in MySQL. after that send that string to android client. extract image string and decode with base 64 decode. after that you will get byte array you can simply show in your image view. for your reference I will show some code

     String imagedata = Base64.encodeToString(thumbnailArray,Base64.DEFAULT);
            mJobject.put("imagebyte",imagedata);
              mJArray.put(mJobject);

            JSONArray json=new JSONArray(response);
            JSONObject jo = null;

           imageArray=new String[json.length()];

         imageArray[i]=jo.getString("imageid");

          completeImage= Base64.decode(imageArray[0],Base64.DEFAULT); 
Bitmap bitmap = BitmapFactory.decodeByteArray(completeImage , 0, completeImage.length);
skyshine
  • 2,767
  • 7
  • 44
  • 84
0

See here is complete code just try this one

Image save to Server

  AsyncTask<Void, Void, HttpEntity> editProfileTask = new AsyncTask<Void, Void, HttpEntity>() {

  @Override
    protected HttpEntity doInBackground(Void... params) {          
     HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("Your url"); // your url

  try {                     
    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("firstname",new StringBody(firstnameEV.getText().toString(),
    "text/plain",Charset.forName("UTF-8")));
               "text/plain",Charset.forName("UTF-8")));
   if (file != null) {
       reqEntity.addPart("image",new FileBody(((File) file),"application/zip"));
      }         
      httppost.setEntity(reqEntity);
       HttpResponse resp = httpclient.execute(httppost);          
     HttpEntity resEntity = resp.getEntity();
     return resEntity;
    } catch (ClientProtocolException e) {
      e.printStackTrace();
     } catch (IOException e) {
     e.printStackTrace();
     }return null;
     }
    @Override
  protected void onPostExecute(HttpEntity resEntity) {
 if (resEntity != null) {             
   try {
     JSONObject responseJsonObject = new JSONObject(EntityUtils.toString(resEntity));
      String status = responseJsonObject.getString("status");             
       if (status.equals("success")) {
            Toast.makeText(activity, "Your Profile is updated", Toast.LENGTH_LONG).show();
             String data = responseJsonObject.getString("data");              
             isUpdatedSuccessfully=true;            
       } else {
          Toast.makeText(activity, "Profile not updated", Toast.LENGTH_LONG).show();
     }
    } catch (Exception e) {
   e.printStackTrace();
  }
    }            
  }
   };
  editProfileTask.execute(null, null, null);
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98