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,027

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



 

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

 



Topic : 109010



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



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




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



Code (Android-Java)
public class ImageUpload extends Activity {
    
	
	private static int RESULT_LOAD_IMAGE = 1;
	String picturePath;
	private EditText tv;
	ProgressDialog dialog = null;
	int serverResponseCode = 0;
	String serverResponseMessage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
        EditText des=(EditText)findViewById(R.id.tv);
        Button b = (Button)findViewById(R.id.upload);	
        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
              
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
        	
			
			public void onClick(View arg0) {
				
				Intent i = new Intent(
						Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				
				startActivityForResult(i, RESULT_LOAD_IMAGE);
			}
		});
    
    
    b.setOnClickListener(new OnClickListener() {           
        
        public void onClick(View v) {
            dialog = ProgressDialog.show(ImageGalleryDemoActivity.this, "", "Uploading file...", true);
             new Thread(new Runnable() {
                    public void run() {
                         runOnUiThread(new Runnable() {
                                public void run() {
                                
                                }
                            });                     
                     int response= uploadFile(picturePath);
                     System.out.println("RES : " + response);                        
                    }
                    
                                 
                                                         
                 
                    public int uploadFile(String sourceFileUri) {
                    	 
                    	
                        String upLoadServerUri = "http://10.0.2.2/rest/new_upload.php";
                        String fileName = sourceFileUri;
                        HttpURLConnection conn = null;
                        DataOutputStream dos = null; 
                        String lineEnd = "\r\n";
                        String twoHyphens = "--";
                        String boundary = "*****";
                        String des=tv.toString();
                        
                        int bytesRead, bytesAvailable, bufferSize;
                        byte[] buffer;
                        int maxBufferSize = 1 * 1024 * 1024;
                        File sourceFile = new File(sourceFileUri);
                        
                        if (!sourceFile.isFile()) {
                         Log.e("uploadFile", "Source File Does not an existan");
                         return 0;
                        }
                            try { // open a URL connection to the Servlet
                             FileInputStream fileInputStream = new FileInputStream(sourceFile);
                             URL url = new URL(upLoadServerUri);
                             conn = (HttpURLConnection) url.openConnection();
                             conn.setDoInput(true); 
                             conn.setDoOutput(true); 
                             conn.setUseCaches(false); 
                             conn.setRequestMethod("POST");
                             conn.setRequestProperty("Connection", "Keep-Alive");
                             conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                             conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                             conn.setRequestProperty("uploaded_file", fileName);
                             conn.setRequestProperty("des", des);
                            
                             dos = new DataOutputStream(conn.getOutputStream());
                   
                             dos.writeBytes(twoHyphens + boundary + lineEnd);
                             dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName+"\"" + lineEnd);
                             dos.writeBytes("Content-Disposition: form-data; name=\"des\";filename=\""+ des+"\"" + lineEnd);
                             dos.writeBytes(lineEnd);
                   
                             bytesAvailable = fileInputStream.available();
                   
                             bufferSize = Math.min(bytesAvailable, maxBufferSize);
                             buffer = new byte[bufferSize];
                             // read file and write it into form...
                             bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
                               
                             while (bytesRead > 0) {
                               dos.write(buffer, 0, bufferSize);
                               bytesAvailable = fileInputStream.available();
                               bufferSize = Math.min(bytesAvailable, maxBufferSize);
                               bytesRead = fileInputStream.read(buffer, 0, bufferSize);              
                              }
                   
                          
                             dos.writeBytes(lineEnd);
                             dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                   
                           
                             serverResponseCode = conn.getResponseCode();
                             serverResponseMessage = conn.getResponseMessage();
                             Map<String, List<String>> temp = conn.getHeaderFields();
                             
                             System.out.println("anand===>"+temp.toString());
                             Log.i("uploadFile", "an class=" + serverResponseMessage + ": " + serverResponseCode);
                             if(serverResponseCode == 200){
                                 runOnUiThread(new Runnable() {
                                      public void run() {
                                         // tv.setText("an File Uploadan> Completed.");
                                          Toast.makeText(ImageGalleryDemoActivity.this, "File Upload Complete."+serverResponseMessage, Toast.LENGTH_SHORT).show();
                                      }
                                  });               
                             }   
                             
                           
                             fileInputStream.close();
                             dos.flush();
                             dos.close();
                              
                        } catch (MalformedURLException ex) { 
                            dialog.dismiss(); 
                            ex.printStackTrace();
                            Toast.makeText(ImageGalleryDemoActivity.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
                            Log.e("Upload file to server", "error: " + ex.getMessage(), ex); 
                        } catch (Exception e) {
                            dialog.dismiss(); 
                            e.printStackTrace();
                            Toast.makeText(ImageGalleryDemoActivity.this, "Exception : " + e.getMessage(), Toast.LENGTH_SHORT).show();
                            Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e); 
                        }
                        dialog.dismiss();      
                        return serverResponseCode; 
                       }
                    
                                                          
                    
                  
                  }).start();       
            }
    });
  }




Tag : Mobile, MySQL, Android, JAVA







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

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 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 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

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