Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > Mobile > Mobile Forum > Android - สอบถาม เรื่องอัพโหลด รูปภาพและ TEXT ไปยัง server คะ (:


[Mobile] Android - สอบถาม เรื่องอัพโหลด รูปภาพและ TEXT ไปยัง server คะ (:

 
Topic : 109010



โพสกระทู้ ( 0 )
บทความ ( 0 )



สถานะออฟไลน์



code นี้สามราถ อัพรูป แล้วเก็บ path ที่อยู่ไว้ใน MySql ได้ แต่ ใน MySql ไม่สามารถเก็บ Text ได้
ผิดตรงไหน หรือควรแก้ไขอะไร ช่วนแนะน้หน่อยนะคะ (:(:



Code (Android-Java)
001.public class ImageUpload extends Activity {
002.     
003.     
004.    private static int RESULT_LOAD_IMAGE = 1;
005.    String picturePath;
006.    private EditText tv;
007.    ProgressDialog dialog = null;
008.    int serverResponseCode = 0;
009.    String serverResponseMessage;
010. 
011.    @Override
012.    public void onCreate(Bundle savedInstanceState) {
013.        super.onCreate(savedInstanceState);
014.        setContentView(R.layout.main);
015.        
016.        
017.        EditText des=(EditText)findViewById(R.id.tv);
018.        Button b = (Button)findViewById(R.id.upload);  
019.        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
020.               
021.        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
022.             
023.             
024.            public void onClick(View arg0) {
025.                 
026.                Intent i = new Intent(
027.                        Intent.ACTION_PICK,
028.                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
029.                 
030.                startActivityForResult(i, RESULT_LOAD_IMAGE);
031.            }
032.        });
033.     
034.     
035.    b.setOnClickListener(new OnClickListener() {          
036.         
037.        public void onClick(View v) {
038.            dialog = ProgressDialog.show(ImageGalleryDemoActivity.this, "", "Uploading file...", true);
039.             new Thread(new Runnable() {
040.                    public void run() {
041.                         runOnUiThread(new Runnable() {
042.                                public void run() {
043.                                 
044.                                }
045.                            });                    
046.                     int response= uploadFile(picturePath);
047.                     System.out.println("RES : " + response);                       
048.                    }
049.                     
050.                                  
051.                                                          
052.                  
053.                    public int uploadFile(String sourceFileUri) {
054.                          
055.                         
056.                        String upLoadServerUri = "http://10.0.2.2/rest/new_upload.php";
057.                        String fileName = sourceFileUri;
058.                        HttpURLConnection conn = null;
059.                        DataOutputStream dos = null;
060.                        String lineEnd = "\r\n";
061.                        String twoHyphens = "--";
062.                        String boundary = "*****";
063.                        String des=tv.toString();
064.                         
065.                        int bytesRead, bytesAvailable, bufferSize;
066.                        byte[] buffer;
067.                        int maxBufferSize = 1 * 1024 * 1024;
068.                        File sourceFile = new File(sourceFileUri);
069.                         
070.                        if (!sourceFile.isFile()) {
071.                         Log.e("uploadFile", "Source File Does not an existan");
072.                         return 0;
073.                        }
074.                            try { // open a URL connection to the Servlet
075.                             FileInputStream fileInputStream = new FileInputStream(sourceFile);
076.                             URL url = new URL(upLoadServerUri);
077.                             conn = (HttpURLConnection) url.openConnection();
078.                             conn.setDoInput(true);
079.                             conn.setDoOutput(true);
080.                             conn.setUseCaches(false);
081.                             conn.setRequestMethod("POST");
082.                             conn.setRequestProperty("Connection", "Keep-Alive");
083.                             conn.setRequestProperty("ENCTYPE", "multipart/form-data");
084.                             conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
085.                             conn.setRequestProperty("uploaded_file", fileName);
086.                             conn.setRequestProperty("des", des);
087.                             
088.                             dos = new DataOutputStream(conn.getOutputStream());
089.                    
090.                             dos.writeBytes(twoHyphens + boundary + lineEnd);
091.                             dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName+"\"" + lineEnd);
092.                             dos.writeBytes("Content-Disposition: form-data; name=\"des\";filename=\""+ des+"\"" + lineEnd);
093.                             dos.writeBytes(lineEnd);
094.                    
095.                             bytesAvailable = fileInputStream.available();
096.                    
097.                             bufferSize = Math.min(bytesAvailable, maxBufferSize);
098.                             buffer = new byte[bufferSize];
099.                             // read file and write it into form...
100.                             bytesRead = fileInputStream.read(buffer, 0, bufferSize);
101.                                
102.                             while (bytesRead > 0) {
103.                               dos.write(buffer, 0, bufferSize);
104.                               bytesAvailable = fileInputStream.available();
105.                               bufferSize = Math.min(bytesAvailable, maxBufferSize);
106.                               bytesRead = fileInputStream.read(buffer, 0, bufferSize);             
107.                              }
108.                    
109.                           
110.                             dos.writeBytes(lineEnd);
111.                             dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
112.                    
113.                            
114.                             serverResponseCode = conn.getResponseCode();
115.                             serverResponseMessage = conn.getResponseMessage();
116.                             Map<String, List<String>> temp = conn.getHeaderFields();
117.                              
118.                             System.out.println("anand===>"+temp.toString());
119.                             Log.i("uploadFile", "an class=" + serverResponseMessage + ": " + serverResponseCode);
120.                             if(serverResponseCode == 200){
121.                                 runOnUiThread(new Runnable() {
122.                                      public void run() {
123.                                         // tv.setText("an File Uploadan> Completed.");
124.                                          Toast.makeText(ImageGalleryDemoActivity.this, "File Upload Complete."+serverResponseMessage, Toast.LENGTH_SHORT).show();
125.                                      }
126.                                  });              
127.                             }  
128.                              
129.                            
130.                             fileInputStream.close();
131.                             dos.flush();
132.                             dos.close();
133.                               
134.                        } catch (MalformedURLException ex) {
135.                            dialog.dismiss();
136.                            ex.printStackTrace();
137.                            Toast.makeText(ImageGalleryDemoActivity.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
138.                            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
139.                        } catch (Exception e) {
140.                            dialog.dismiss();
141.                            e.printStackTrace();
142.                            Toast.makeText(ImageGalleryDemoActivity.this, "Exception : " + e.getMessage(), Toast.LENGTH_SHORT).show();
143.                            Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e);
144.                        }
145.                        dialog.dismiss();     
146.                        return serverResponseCode;
147.                       }
148.                     
149.                                                           
150.                     
151.                   
152.                  }).start();      
153.            }
154.    });
155.  }




Tag : Mobile, MySQL, Android, JAVA

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2014-06-08 16:29:46 By : sleeppiiz View : 1040 Reply : 1
 

 

No. 1



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Quote:
String upLoadServerUri = "http://10.0.2.2/rest/new_upload.php";


ส่งเป็น GET ไปครับ เช่น

String upLoadServerUri = "http://10.0.2.2/rest/new_upload.php?var=1234&var2=5678";

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-06-09 09:23:13 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Android - สอบถาม เรื่องอัพโหลด รูปภาพและ TEXT ไปยัง server คะ (:
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่