/[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 595 - (hide annotations) (download)
Sat Feb 20 12:44:43 2010 UTC (14 years, 3 months ago) by torben
File size: 4415 byte(s)
First version of picturePoster
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     import org.apache.http.NameValuePair;
9     import org.apache.http.client.HttpClient;
10     import org.apache.http.client.methods.HttpPost;
11     import org.apache.http.client.utils.URLEncodedUtils;
12     import org.apache.http.entity.mime.MultipartEntity;
13     import org.apache.http.entity.mime.content.FileBody;
14     import org.apache.http.entity.mime.content.StringBody;
15     import org.apache.http.impl.client.DefaultHttpClient;
16     import org.apache.http.message.BasicNameValuePair;
17    
18     import android.app.Activity;
19     import android.content.Intent;
20     import android.database.Cursor;
21     import android.net.Uri;
22     import android.os.Bundle;
23     import android.util.Log;
24     import android.view.View;
25     import android.view.View.OnClickListener;
26     import android.widget.Button;
27     import android.widget.ImageView;
28     import android.widget.TextView;
29     import android.widget.Toast;
30    
31     public class PostActivity extends Activity {
32     static final int IMAGE_PICK = 1000;
33    
34     ImageView picture;
35     String media_path;
36     TextView title;
37     TextView note;
38    
39     /** Called when the activity is first created. */
40     @Override
41     public void onCreate(Bundle savedInstanceState) {
42     super.onCreate(savedInstanceState);
43     setContentView(R.layout.main);
44    
45     picture = (ImageView) findViewById(R.id.picture);
46     title = (TextView) findViewById(R.id.title);
47     note = (TextView) findViewById(R.id.note);
48    
49     Button changePicture = (Button) findViewById(R.id.changePicture);
50     changePicture.setOnClickListener( new OnClickListener() {
51     public void onClick(View v) {
52     changePicture();
53     }
54     });
55    
56     Button submit = (Button) findViewById(R.id.submit);
57     submit.setOnClickListener( new OnClickListener() {
58     public void onClick(View v) {
59     submit();
60     }
61     });
62     }
63    
64    
65    
66    
67     @Override
68     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
69     super.onActivityResult(requestCode, resultCode, data);
70    
71     switch(requestCode) {
72     case IMAGE_PICK:
73     imagePicked(resultCode,data);
74     break;
75     }
76    
77     }
78    
79    
80     void imagePicked(int resultCode,Intent data) {
81     Log.i("PostActivity", "ResultCode " + resultCode);
82    
83     if (resultCode != 0) {
84     Log.i("PostActivity", data.getDataString());
85     Cursor c = managedQuery(data.getData(),null,null,null,null) ;
86    
87     if( c.moveToFirst() ) {
88     media_path = c.getString(1) ;
89     Log.i("PostActivity", media_path);
90     picture.setImageURI( Uri.parse(media_path) );
91     } else {
92     picture.setImageDrawable(null);
93     }
94     }
95     }
96    
97     void changePicture() {
98    
99     Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
100     intent.setType("image/*");
101     startActivityForResult(intent, IMAGE_PICK);
102     }
103    
104     void submit() {
105     HttpPost post = new HttpPost("http://app.t-hoerup.dk/PicturePosterService/PostServlet");
106     String titleStr = title.getText().toString();
107     String noteStr = note.getText().toString();
108    
109     if (titleStr.trim().length() == 0) {
110     Toast.makeText(this, "Please enter a title", Toast.LENGTH_LONG).show();
111     return;
112     }
113     if (noteStr.trim().length() == 0) {
114     Toast.makeText(this, "Please enter a note", Toast.LENGTH_LONG).show();
115     return;
116     }
117    
118     if (media_path == null || media_path.length() == 0) {
119     Toast.makeText(this, "Please select an picture", Toast.LENGTH_LONG).show();
120     return;
121     }
122    
123    
124    
125     MultipartEntity entity = new MultipartEntity();
126    
127     entity.addPart("file", new FileBody( new File(media_path), "image/jpeg") );
128    
129     try {
130     List<NameValuePair> data = new ArrayList<NameValuePair>();
131     data.add( new BasicNameValuePair("title", titleStr) );
132     data.add( new BasicNameValuePair("note", noteStr) );
133     String encoded = URLEncodedUtils.format(data, "UTF-8");
134     entity.addPart("text", new StringBody(encoded, URLEncodedUtils.CONTENT_TYPE, Charset.forName("UTF-8") ));
135     } catch (Exception e) {
136     Log.e("asdasd","asdasdasd",e);
137     }
138    
139    
140     post.setEntity(entity);
141    
142     HttpClient client = new DefaultHttpClient();
143     try {
144     client.execute(post);
145     Toast.makeText(this, "Upload succeeded", Toast.LENGTH_LONG).show();
146    
147     } catch (Exception e) {
148     Log.d("PostActivity", "upload failed", e);
149     Toast.makeText(this, "Upload failed", Toast.LENGTH_LONG).show();
150     }
151     }
152    
153     }

  ViewVC Help
Powered by ViewVC 1.1.20