/[projects]/android/PicturePoster/src/dk/thoerup/pictureposter/PostActivity.java
ViewVC logotype

Annotation of /android/PicturePoster/src/dk/thoerup/pictureposter/PostActivity.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 604 - (hide annotations) (download)
Sun Feb 21 20:58:36 2010 UTC (14 years, 3 months ago) by torben
File size: 6752 byte(s)
Use rafiki as server
1 torben 595 package dk.thoerup.pictureposter;
2    
3     import java.io.File;
4     import java.nio.charset.Charset;
5     import java.util.ArrayList;
6     import java.util.List;
7    
8 torben 596 import org.apache.http.HttpResponse;
9 torben 595 import org.apache.http.NameValuePair;
10     import org.apache.http.client.HttpClient;
11     import org.apache.http.client.methods.HttpPost;
12     import org.apache.http.client.utils.URLEncodedUtils;
13     import org.apache.http.entity.mime.MultipartEntity;
14     import org.apache.http.entity.mime.content.FileBody;
15     import org.apache.http.entity.mime.content.StringBody;
16     import org.apache.http.impl.client.DefaultHttpClient;
17     import org.apache.http.message.BasicNameValuePair;
18    
19     import android.app.Activity;
20 torben 598 import android.app.Dialog;
21     import android.app.ProgressDialog;
22 torben 595 import android.content.Intent;
23     import android.database.Cursor;
24 torben 598 import android.location.Criteria;
25     import android.location.Location;
26     import android.location.LocationListener;
27     import android.location.LocationManager;
28 torben 595 import android.net.Uri;
29 torben 598 import android.os.AsyncTask;
30 torben 595 import android.os.Bundle;
31     import android.util.Log;
32     import android.view.View;
33     import android.view.View.OnClickListener;
34     import android.widget.Button;
35     import android.widget.ImageView;
36     import android.widget.TextView;
37     import android.widget.Toast;
38    
39 torben 598 public class PostActivity extends Activity implements LocationListener {
40 torben 595 static final int IMAGE_PICK = 1000;
41    
42     ImageView picture;
43     String media_path;
44     TextView title;
45     TextView note;
46    
47 torben 598 Location currentLocation;
48     LocationManager locManager;
49    
50 torben 595 /** Called when the activity is first created. */
51     @Override
52     public void onCreate(Bundle savedInstanceState) {
53     super.onCreate(savedInstanceState);
54     setContentView(R.layout.main);
55    
56     picture = (ImageView) findViewById(R.id.picture);
57     title = (TextView) findViewById(R.id.title);
58     note = (TextView) findViewById(R.id.note);
59    
60     Button changePicture = (Button) findViewById(R.id.changePicture);
61     changePicture.setOnClickListener( new OnClickListener() {
62     public void onClick(View v) {
63     changePicture();
64     }
65     });
66    
67     Button submit = (Button) findViewById(R.id.submit);
68     submit.setOnClickListener( new OnClickListener() {
69     public void onClick(View v) {
70     submit();
71     }
72     });
73 torben 598
74     locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
75     Criteria c = new Criteria();
76     c.setAccuracy(Criteria.ACCURACY_FINE);
77     String provider = locManager.getBestProvider(c, true);
78     locManager.requestLocationUpdates(provider, 10, 0.0f, this);
79    
80 torben 595 }
81    
82    
83    
84    
85     @Override
86     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
87     super.onActivityResult(requestCode, resultCode, data);
88    
89     switch(requestCode) {
90     case IMAGE_PICK:
91     imagePicked(resultCode,data);
92     break;
93     }
94    
95     }
96    
97    
98     void imagePicked(int resultCode,Intent data) {
99     Log.i("PostActivity", "ResultCode " + resultCode);
100    
101     if (resultCode != 0) {
102     Log.i("PostActivity", data.getDataString());
103     Cursor c = managedQuery(data.getData(),null,null,null,null) ;
104    
105     if( c.moveToFirst() ) {
106     media_path = c.getString(1) ;
107     Log.i("PostActivity", media_path);
108     picture.setImageURI( Uri.parse(media_path) );
109     } else {
110     picture.setImageDrawable(null);
111     }
112     }
113     }
114    
115     void changePicture() {
116    
117     Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
118     intent.setType("image/*");
119     startActivityForResult(intent, IMAGE_PICK);
120     }
121    
122     void submit() {
123     String titleStr = title.getText().toString();
124     String noteStr = note.getText().toString();
125    
126     if (titleStr.trim().length() == 0) {
127 torben 598 Toast.makeText(PostActivity.this, "Please enter a title", Toast.LENGTH_LONG).show();
128 torben 595 return;
129     }
130     if (noteStr.trim().length() == 0) {
131 torben 598 Toast.makeText(PostActivity.this, "Please enter a note", Toast.LENGTH_LONG).show();
132 torben 595 return;
133     }
134    
135     if (media_path == null || media_path.length() == 0) {
136 torben 598 Toast.makeText(PostActivity.this, "Please select an picture", Toast.LENGTH_LONG).show();
137     return;
138 torben 595 }
139    
140 torben 598 UploaderTask task = new UploaderTask();
141     task.execute();
142     }
143 torben 595
144 torben 598
145    
146    
147    
148     @Override
149     public void onLocationChanged(Location location) {
150     currentLocation = location;
151     locManager.removeUpdates(this);
152     }
153    
154     @Override
155     public void onProviderDisabled(String provider) {
156     }
157    
158     @Override
159     public void onProviderEnabled(String provider) {
160     }
161    
162     @Override
163     public void onStatusChanged(String provider, int status, Bundle extras) {
164     }
165    
166     class UploaderTask extends AsyncTask<Void, Void, Boolean> {
167     String titleStr;
168     String noteStr;
169    
170     Dialog dlg;
171    
172     @Override
173     protected void onPreExecute() {
174     super.onPreExecute();
175     titleStr = title.getText().toString();
176     noteStr = note.getText().toString();
177    
178     dlg = ProgressDialog.show(PostActivity.this, "PicturePoster", "Uploading data");
179 torben 595 }
180 torben 598
181     @Override
182     protected Boolean doInBackground(Void... params) {
183 torben 604 HttpPost post = new HttpPost("http://app.t-hoerup.dk/PicturePosterService/PostServlet");
184 torben 598
185    
186    
187     MultipartEntity entity = new MultipartEntity();
188    
189     entity.addPart("file", new FileBody( new File(media_path), "image/jpeg") );
190    
191     try {
192     boolean hasLoc = currentLocation != null;
193     List<NameValuePair> data = new ArrayList<NameValuePair>();
194     data.add( new BasicNameValuePair("title", titleStr) );
195     data.add( new BasicNameValuePair("note", noteStr) );
196    
197 torben 600 data.add( new BasicNameValuePair("latitude", hasLoc ? Float.toString((float)currentLocation.getLatitude()) : "") );
198     data.add( new BasicNameValuePair("longitude", hasLoc ? Float.toString((float)currentLocation.getLongitude()) : "" ) );
199 torben 598
200     String encoded = URLEncodedUtils.format(data, "UTF-8");
201     entity.addPart("text", new StringBody(encoded, URLEncodedUtils.CONTENT_TYPE, Charset.forName("UTF-8") ));
202     } catch (Exception e) {
203     Log.e("asdasd","asdasdasd",e);
204 torben 596 }
205 torben 595
206 torben 596
207 torben 598 post.setEntity(entity);
208     boolean ok = false;
209     HttpClient client = new DefaultHttpClient();
210     try {
211     HttpResponse resp = client.execute(post);
212     Log.d("PostActivity", "" + resp.getStatusLine().getStatusCode() + ": " + resp.getStatusLine().getReasonPhrase() );
213     if (resp.getStatusLine().getStatusCode() == 200) {
214     ok = true;
215     }
216    
217     } catch (Exception e) {
218     Log.d("PostActivity", "upload failed", e);
219     }
220     return ok;
221    
222 torben 596 }
223 torben 598 @Override
224     protected void onPostExecute(Boolean result) {
225     super.onPostExecute(result);
226    
227     dlg.dismiss();
228    
229     if (result == true) {
230     Toast.makeText(PostActivity.this, "Upload succeeded", Toast.LENGTH_LONG).show();
231     } else {
232     Toast.makeText(PostActivity.this, "Upload failed", Toast.LENGTH_LONG).show();
233     }
234 torben 595 }
235     }
236     }

  ViewVC Help
Powered by ViewVC 1.1.20