วันนี้ผมจะมานำเสนอวิธีการเขียนโค้ดอับโหลดรูป หรือ อับโหลดวิดีโอ จากเจ้า Android ขึ้นสู่ยอดเสา เอ้ย!! ขึ้น Server กันนะครับ

โดยพระเอกหลักของงานนี้ คือ Class HttpClient ครับ ซึ่งเป็น Free library จาก Apache ครับ
รายละเอียดเพิ่มเติม : http://hc.apache.org/index.html

Step ที่ 1 ต้องมี HttpClient library ก่อนครับ โดยไป Download จากลิงค์ด้านบนได้เลยครับ เมื่อแตก .zip ออกมาจะอยู่ใน folder lib ครับ จะเห็นไฟล์ตามรูปนี้ครับ
ให้ copy 3 ไฟล์ที่ขีดเส้นสีแดงไปไว้ใน folder libs ใน Project ของเราครับ

Step ที่ 2 เริ่ม Coding กันเลย
**ขอข้ามเรื่อง UI กับ การใช้ Intent ไปเลยนะครับ สามารถไปดูได้ในบทความที่ผ่านมาครับ
[Android Tips] วิธีเรียกใช้ Intent เพื่อเลือกรูปภาพหรือวีดิโอ

ก่อนอื่นสร้าง Classs Uploader ซึ่งเป็น AsyncTask ขึ้นมาก่อนนะครับ
อะไรคือ AsyncTask : http://devahoy.com/2014/05/android-asynctask-tutorial/
public class Uploader extends  AsyncTask<Object, Void, String> { 
   
        ProgressDialog progressDialog; 
   
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result); 
               
                progressDialog.dismiss(); 
            if (result != null) { 
                Toast.makeText(getApplicationContext(), "Response :" + result, Toast.LENGTH_LONG).show(); 
   
            } 
        } 
   
        @Override
        protected void onPreExecute() { 

            progressDialog = ProgressDialog.show(MainActivity.this, "", "Uploading video.. "); 
            super.onPreExecute(); 
        } 
   
        @Override
        protected String doInBackground(String... params) { 
            String result_data = ""; 
            String url = "Enter URL here"; 
            try { 
                HttpClient httpclient = new DefaultHttpClient(); 
                HttpPost httppost = new HttpPost(url); 
 
 
                /*** วิดีโอใช้ Code นี้ ***/
                File input = new File(params[0]); 
                FileBody filebodyVideo = new FileBody(input); 
                StringBody title = new StringBody(params[0]); 
                StringBody username = new StringBody(urUname); 
                StringBody password = new StringBody(urPassword); 
                MultipartEntity reqEntity = new MultipartEntity(); 
                reqEntity.addPart("videoFile", filebodyVideo); 
                /*****************/
 
 
                /*** รูปใช้ Code นี้ ***/
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                Bitmap bitmap = BitmapFactory.decodeFile(params[0]);
                bitmap.compress(CompressFormat.JPEG, 80, bos);
                byte[] data = bos.toByteArray();
                entity.addPart("imageFile", new ByteArrayBody(data, "image name"));
                /*****************/
 
                httppost.setEntity(reqEntity); 
                HttpResponse response = httpclient.execute(httppost); 
                HttpEntity resEntity = response.getEntity(); 
                String code_message = response.getStatusLine() + "///"; 
                System.out.println(code_message + ""); 
   
                if (resEntity != null) { 
                    publishProgress(100); 
                    String result_message = EntityUtils.toString(resEntity); 
                    resEntity.consumeContent(); 
                    System.out.println(result_message + ""); 
                    code_message = code_message + result_message; 
                } 
                httpclient.getConnectionManager().shutdown(); 
                result_data = code_message; 
   
            } catch (Exception e) { 
                Log.e("Exception", e + ""); 
                result_data = null; 
            } 
   
            return result_data; 
        } 
}


การใช้งานก็ไม่ยากครับก็ตามนี้เลย
new Uploader().execute("Path ของรูปหรือวิดีโอ"); 


ซึ่ง Path ของรูปหรือวิดีโอนั้นก็หาได้จาก Method ดังต่อไปนี้ได้เลยครับ โดยการส่ง Uri เข้าไป
private String getPath(Uri uri) { 
        String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION }; 
        @SuppressWarnings("deprecation") 
        Cursor cursor = managedQuery(uri, projection, null, null, null); 
        cursor.moveToFirst(); 
        String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)); 
        int fileSize = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)); 
        long duration = TimeUnit.MILLISECONDS.toSeconds(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION))); 
        System.out.println("size: " + fileSize); 
        System.out.println("path: " + filePath); 
        System.out.println("duration: " + duration); 
  
        return filePath; 
}


Ref
http://www.verious.com/tutorial/upload-video-in-server-using-multipart-entity-in-android/
http://hmkcode.com/android-send-json-data-to-server/
http://hc.apache.org/index.html