|
|
|
Android - ช่วยด้วยครับ ผมจะให้ถ่ายรูปแล้ว upload ขึ้น server ยังไงครับ |
|
|
|
|
|
|
|
ตอนนี้ผมทำให้ถ่ายรูปได้แล้วครับ แต่ทีนี้ผมจะทำให้รูปอัพขึ้น server ยังไงครับ code อยู่ด้านล่างเลยครับ
อัพขึ้น server จะอยู่ในส่วนของ public boolean SaveData()
public class TakePhoto <export_main_activity> extends Activity implements View.OnClickListener{
protected Button btnTakePhoto, btnUpload;
protected ImageView imageview;
protected String path_pic = null;
protected boolean taken;
protected static final String PHOTO_TAKEN = "photo_taken";
String up_name;
Bitmap bitmap = null;
TextView txtPhotoName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_takephoto);
txtPhotoName = (TextView)findViewById(R.id.txtPhotoName);
imageview = ( ImageView ) findViewById( R.id.imagePOI );
btnTakePhoto = ( Button ) findViewById( R.id.btnTakePhoto );
btnTakePhoto.setOnClickListener( this);
btnUpload = ( Button ) findViewById( R.id.btnUpload);
btnUpload.setOnClickListener(this);
File file = new File(Environment.getExternalStorageDirectory() + "/myPhoto/");
if (!file.exists()) {
try{
file.mkdirs();
}catch(Exception e){
e.printStackTrace();
}
}
}///// end onCreate //////
@Override
public void onClick(View v) {
if(v.getId()==R.id.btnTakePhoto){
startCameraActivity();
}
if(v.getId()==R.id.btnUpload){
if(SaveData())
{
// When Save Complete
}
}
}
protected void startCameraActivity(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String picTime = sdf.format(new Date(100));
path_pic = Environment.getExternalStorageDirectory() + "/TravelThailandPhoto/"+picTime+".jpg";
up_name = picTime+".jpg";
File file = new File(path_pic);
Uri outputFileUri = Uri.fromFile( file );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult( intent, 0 );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//Log.i( "resultcode", "resultCode: " + resultCode );
switch( resultCode )
{
case 0:
Log.i( "Tag", "User cancelled" );
break;
case -1:
onPhotoTaken();
break;
}
}
protected void onPhotoTaken(){
taken = true;
bitmap = decodeFile(path_pic);
imageview.setImageBitmap(bitmap);
}
@Override
protected void onSaveInstanceState( Bundle outState ) {
outState.putBoolean( TakePhoto.PHOTO_TAKEN, taken );
}
@Override
protected void onRestoreInstanceState( Bundle savedInstanceState){
Log.i( "Tag", "onRestoreInstanceState()");
if( savedInstanceState.getBoolean( TakePhoto.PHOTO_TAKEN ) ) {
onPhotoTaken();
}
}
public Bitmap 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 bmp = BitmapFactory.decodeFile(filePath, o2);
return bmp;
//imgView.setImageBitmap(bitmap);
}
public boolean SaveData()
{
// txtUsername,txtPassword,txtName,txtEmail,txtTel
final EditText txtPhotoName = (EditText) findViewById(R.id.txtPhotoName);
final EditText txtPhotoDetail = (EditText) findViewById(R.id.txtPhotoDetail);
String url = "http://travelthailand.incasible.com/mobile_savepoi.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tPhotoName", txtPhotoName.getText().toString()));
params.add(new BasicNameValuePair("tPhotoDetail", txtPhotoDetail.getText().toString()));
// params.add(new BasicNameValuePair("tMid", MemID.toString()));
String resultServer = Helper.getHttpPost(url,params);
/*** Default Value ***/
String strStatusID = "0";
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(TakePhoto.this, "บันทึก Photo เรียบร้อยแล้ว.", Toast.LENGTH_SHORT).show();
txtPhotoName.setText("");
}
return true;
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Tag : Mobile, Android, JAVA
|
|
|
|
|
|
Date :
2014-03-14 17:03:43 |
By :
gungsakab |
View :
1095 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คือผมไม่ทราบว่าจะนำรูป ขึ้น server ยังไง
|
|
|
|
|
Date :
2014-03-14 17:06:42 |
By :
gungsakab |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอนนี้ทำได้แล้วนะครับ ส่วน code โปรแกรมทั้งหมด ถ้าทำ Project เสร็จ จะเอามาแจกครับ
ถ้าไม่ติดกฏของทางมหาลัย นะครับ
|
|
|
|
|
Date :
2014-03-14 23:11:23 |
By :
gungsakab |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Android Upload Send file to Web Server (Website) -
สำหรับ Code าร Upload ครับ
|
|
|
|
|
Date :
2014-03-17 06:41:18 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|