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 > Andorid - ไม่สามารถอัพโหลด Upload รูปภาพขึ้น Server บน PHP ได้ รบกวนพี่แอดมินดูให้ทีนะครับ



 

Andorid - ไม่สามารถอัพโหลด Upload รูปภาพขึ้น Server บน PHP ได้ รบกวนพี่แอดมินดูให้ทีนะครับ

 



Topic : 104885



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



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




Code (Android-Java)
package com.softeng.myproject;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AddTemple extends Activity {
	private static final int PICK_IMAGE = 1;
	private ImageView imgView;
	private Button upload;
	private EditText caption;
	private Bitmap bitmap;
	private ProgressDialog dialog;
	private TextView tv_file;
	final String url = "http://appsofteng.com/student7/Project/addtemple.php";
	String file_name;
  
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_temple);
        
        tv_file = (TextView)findViewById(R.id.tv_file);
		tv_file.setText("");
		imgView = (ImageView) findViewById(R.id.ImageView);
		upload = (Button) findViewById(R.id.Upload);
		caption = (EditText) findViewById(R.id.Caption);

        // Permission StrictMode
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        
        //btnBack
        final Button btnBack = (Button) findViewById(R.id.btnBack);
        btnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent ittemple = new Intent (getApplicationContext(),ManagerTemple.class);
				startActivity(ittemple);
			}
		});
        
        // btnSave
        final Button btnSave = (Button) findViewById(R.id.btnSave);
        // Perform action on click
        btnSave.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(SaveData())
            	{
            		// When Save Complete
            	}
            }	
        });
    }
   
    public boolean SaveData()
    {	
        final EditText txtName = (EditText) findViewById(R.id.txtName);
        final EditText txtAddress = (EditText) findViewById(R.id.txtAddress);
        final EditText txtLat  = (EditText) findViewById(R.id.txtLat);
        final EditText txtLong = (EditText) findViewById(R.id.txtLong);
        
        upload.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(Intent.createChooser(intent, "Select Picture"),PICK_IMAGE);
			}
        });
			
           
    		// Dialog
        	final AlertDialog.Builder ad = new AlertDialog.Builder(this);

    		ad.setTitle("Error! ");
    		ad.setIcon(android.R.drawable.btn_star_big_on); 
    		ad.setPositiveButton("Close", null);

    		// Check Name
    		if(txtName.getText().length() == 0)
    		{
                ad.setMessage("Please input [Name] ");
                ad.show();
                txtName.requestFocus();
                return false;
    		}
    		
    		// Check Lat
    		if(txtLat.getText().length() == 0)
    		{
                ad.setMessage("Please input [Lattitude] ");
                ad.show();
                txtLat.requestFocus();
                return false;
    		}
    		
    		// Check Long
    		if(txtLong.getText().length() == 0)
    		{
                ad.setMessage("Please input [Longtitude] ");
                ad.show();
                txtLong.requestFocus();
                return false;
    		}
    
     		Log.d("OK","URL SUCCESS");
    		List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("sName", txtName.getText().toString()));
            params.add(new BasicNameValuePair("sAddress", txtAddress.getText().toString()));
            params.add(new BasicNameValuePair("sLattitude", txtLat.getText().toString()));
            params.add(new BasicNameValuePair("sLongtitude", txtLong.getText().toString()));
            
        	String resultServer  = getHttpPost(url,params);
            
            /*** Default Value ***/
        	String strStatusID = "1";
        	String strError = "Unknow Status!";
        	
        	JSONObject c;
			try {
				c = new JSONObject(resultServer);
            	strStatusID = c.getString("StatusID");
            	strError = c.getString("Error");
            	
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			// Prepare Save Data
			if(strStatusID.equals("0"))
			{
                ad.setMessage(strError);
                ad.show();
			}
			else
			{
				Toast.makeText(AddTemple.this, "Save Data Successfully", Toast.LENGTH_LONG).show();
				Intent it1 = new Intent (AddTemple.this,ManagerTemple.class);
				startActivity(it1);
				txtName.setText("");
				txtAddress.setText("");
				txtLat.setText("");
				txtLong.setText("");
			}
       	          
    	return true;
    }
   

	public String getHttpPost(String url,List<NameValuePair> params) {
		StringBuilder str = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
				
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
			HttpResponse response = client.execute(httpPost);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200) { // Status OK
				HttpEntity entity = response.getEntity();
				InputStream content = entity.getContent();
				BufferedReader reader = new BufferedReader(new InputStreamReader(content));
				String line;
				while ((line = reader.readLine()) != null) {
					str.append(line);
				}
			} else {
				Log.e("Log", "Failed to download result..");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str.toString();
	}
	

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case PICK_IMAGE:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImageUri = data.getData();
                String filePath = null;
 
                try {
                     
                    String filemanagerstring = selectedImageUri.getPath();
 
                     
                    String selectedImagePath = getPath(selectedImageUri);
 
                    if (selectedImagePath != null) {
                        filePath = selectedImagePath;
                    } else if (filemanagerstring != null) {
                        filePath = filemanagerstring;
                    } else {
                        Toast.makeText(getApplicationContext(), "Unknown path",
                                Toast.LENGTH_LONG).show();
                        Log.e("Bitmap", "Unknown path");
                    }
 
                    if (filePath != null) {
                        decodeFile(filePath);
                        tv_file.setText(filePath);
                        File file = new File(filePath);
                        file_name=file.getName();
                         
                    } else {
                        bitmap = null;
                    }
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Internal error",
                            Toast.LENGTH_LONG).show();
                    Log.e(e.getClass().getName(), e.getMessage(), e);
                }
            }
            break;
        default:
        }
    }
    
    class ImageUploadTask extends AsyncTask <Void, Void, String>{
        String err=null;
        @Override
        protected String doInBackground(Void... unsued) {
             
            try {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmap.compress(CompressFormat.JPEG, 75, bos);
                byte[] data = bos.toByteArray();
                HttpClient httpClient = new DefaultHttpClient();                
                HttpPost postRequest = new HttpPost(url);               
                ByteArrayBody bab = new ByteArrayBody(data,file_name);              
                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                reqEntity.addPart("sImage", bab);
                reqEntity.addPart("Image", new StringBody(caption.getText().toString().trim()));
                postRequest.setEntity(reqEntity);
                HttpResponse response = httpClient.execute(postRequest);
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
                String sResponse;
                StringBuilder s = new StringBuilder();
 
                while ((sResponse = reader.readLine()) != null) {
                    s = s.append(sResponse);
                }
                 
                return s.toString().trim();
             
            } catch (Exception e) {
                 
                err="error"+e.getMessage();
                Log.e(e.getClass().getName(), e.getMessage());
                 
                return e.getMessage();
            }               
        }
        
        protected void onPostExecute(String res) {          
            if (dialog.isShowing())dialog.dismiss();
            AlertDialog.Builder alertbox = new AlertDialog.Builder(AddTemple.this);
            alertbox.setTitle("Information");
            alertbox.setNeutralButton("Ok",null);               
            if(err!=null){
                 alertbox.setMessage("เกิดข้อผิดพลาด!!!\n"+res);    
            }else{                                    
                 alertbox.setMessage(res);                
            }
             alertbox.show();
    }
}
    

	public String getPath(Uri url) {
    	String[] projection = { MediaStore.Images.Media.DATA };
    	Cursor cursor = managedQuery(url, projection, null, null, null);
    	if (cursor != null) {
        // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
        // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else{
        return null;
    }
}

	public void decodeFile(String filePath) {
    // Decode image size
		BitmapFactory.Options o = new BitmapFactory.Options();
		o.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bitmap = BitmapFactory.decodeFile(filePath, o2);
     
    imgView.setImageBitmap(bitmap);

}
}


พี่แอดมินช่วยดูโค๊ดให้หน่อยทำไม ผมถึงอัพโหลดรูปภาพขึ้น server ไม่ได้ครับ

แต่เพิ่มข้อมูลได้อะครับ ชื่อ ที่อยู่ ละติจูด ลองติจูด


ภาพ1



Tag : Mobile, MySQL, Android







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2014-01-22 11:44:10 By : phuwadonlove View : 1638 Reply : 10
 

 

No. 1



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

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

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

โค๊ดยาวเบื้ยยยยยย เยยยย

มันติดตรงสิทธิหรือเปล่าครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-01-22 13:27:13 By : Dragons_first
 


 

No. 2



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



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


ยังไงหรือครับ ติดตรงสิทธิ รบกวนอธิบายหน่อยได้ไหมครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-01-22 13:45:02 By : phuwadonlove
 

 

No. 3



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



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


มีใครพอสามารถช่วยผมไหมคร้าบ T^T
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-01-23 21:46:24 By : phuwadonlove
 


 

No. 4



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

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

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

Quote:
http://appsofteng.com/student7/Project/addtemple.php


ลองสร้าง Form ของ php แล้วทดสอบการส่งค่าดูครับ ว่ามันได้หรือไม่
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-01-24 09:22:27 By : mr.win
 


 

No. 5



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



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


ผมลองสร้างแล้วครับมันสามารถอัพได้คับ แต่ใน Android ไม่สามารถอัพได้อะครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-10 13:38:32 By : phuwadonlove
 


 

No. 6



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



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

แล้วเรื่องการกำหนดขนาดของรูปมีส่วนเกี่ยวข้องกันไหมลองคิดดูน่ะครับ..บางครั้งขนาดส่งจากมือถือไฟล์ใหญ่เกินไปหรือเปล่า
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-26 07:52:43 By : manasak19
 


 

No. 7



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

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

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

จาก Error เหมือนว่า JSON มันแปลงค่าผิด อาจจะเกิดจากส่งค่ากลับมาผิดพลาดครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-26 08:34:39 By : mr.win
 


 

No. 8



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



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


น่าจะเออเร่อที่ มันไม่สามารถ convert string เป็น json object ป่าวคับลองเอา เออเร่อไปเสิทใน google ดูคับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-26 22:05:37 By : kaisiamza
 


 

No. 9



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



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


มีวิธีแก้ไขอย่างไรบ้างครับ รบกวนด้วยนะครับ
ทักเฟสผมก็ได้ [email protected]

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);

ผมสงสัยว่ามันไม่มีคำสั่งนี้หรือเปล่า เหมือน PHP อะครับ

ไม่มี mutipart/form-data มันก็ไม่สามารถอัพรูปได้
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-28 14:43:26 By : phuwadonlove
 


 

No. 10



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

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

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


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-28 17:41:52 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Andorid - ไม่สามารถอัพโหลด Upload รูปภาพขึ้น Server บน PHP ได้ รบกวนพี่แอดมินดูให้ทีนะครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 อัตราราคา คลิกที่นี่