Permalink
Browse files

rev 1

  • Loading branch information...
1 parent 1fd1f09 commit cf1f8a03b84040b4825b59fbd7fe882c825a6755 @markchang committed Mar 20, 2011
@@ -0,0 +1,9 @@
+<component name="libraryTable">
+ <library name="Apache HTTP">
+ <CLASSES>
+ <root url="jar://$PROJECT_DIR$/libs/httpmime-4.1.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+</component>
View
@@ -23,22 +23,6 @@
<option name="LOCALE" />
<option name="OPEN_IN_BROWSER" value="true" />
</component>
- <component name="ProjectInspectionProfilesVisibleTreeState">
- <entry key="Project Default">
- <profile-state>
- <expanded-state>
- <State>
- <id>Inspections</id>
- </State>
- </expanded-state>
- <selected-state>
- <State>
- <id>Abstraction issues</id>
- </State>
- </selected-state>
- </profile-state>
- </entry>
- </component>
<component name="ProjectResources">
<default-html-doctype>http://www.w3.org/1999/xhtml</default-html-doctype>
</component>
View
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
- <mapping directory="" vcs="" />
+ <mapping directory="" vcs="Git" />
</component>
</project>
View
Oops, something went wrong.
View
@@ -7,7 +7,10 @@
<uses-feature android:name="android.hardware.camera"></uses-feature>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
- <application android:label="Andgram" android:icon="@drawable/icon">
+ <application android:label="Andgram"
+ android:icon="@drawable/icon"
+ android:theme="@android:style/Theme.Light.NoTitleBar"
+ android:debuggable="true">
<activity android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
@@ -33,6 +33,7 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Android 2.2 Google APIs" level="application" />
+ <orderEntry type="library" name="Apache HTTP" level="project" />
</component>
</module>
@@ -14,9 +14,11 @@
public static final int icon=0x7f020000;
}
public static final class id {
- public static final int btnLogin=0x7f050002;
- public static final int btnPicture=0x7f050003;
- public static final int imageView=0x7f050004;
+ public static final int btnLogin=0x7f050003;
+ public static final int btnPicture=0x7f050004;
+ public static final int btnUpload=0x7f050005;
+ public static final int imageView=0x7f050006;
+ public static final int txtComment=0x7f050002;
public static final int txtPassword=0x7f050001;
public static final int txtUsername=0x7f050000;
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -4,40 +4,33 @@
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
+import android.net.ParseException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
+import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
-import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
-import org.apache.http.conn.ClientConnectionManager;
-import org.apache.http.conn.scheme.PlainSocketFactory;
-import org.apache.http.conn.scheme.Scheme;
-import org.apache.http.conn.scheme.SchemeRegistry;
-import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.cookie.Cookie;
-import org.apache.http.impl.client.BasicResponseHandler;
+import org.apache.http.entity.mime.HttpMultipartMode;
+import org.apache.http.entity.mime.MultipartEntity;
+import org.apache.http.entity.mime.content.FileBody;
+import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
-import org.apache.http.params.BasicHttpParams;
-import org.apache.http.params.HttpParams;
-import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
-import org.apache.http.util.EntityUtils;
import java.io.File;
import java.util.ArrayList;
@@ -57,9 +50,11 @@
EditText txtPassword = null;
EditText txtUsername = null;
ImageView imageView = null;
+ Button uploadButton = null;
private DefaultHttpClient httpClient = null;
- private Uri imageUri;
+ private Uri imageUri = null;
+ private boolean imageReady = false;
/** Called when the activity is first created. */
@Override
@@ -74,6 +69,8 @@ public void onCreate(Bundle savedInstanceState)
txtPassword = (EditText)findViewById(R.id.txtPassword);
txtUsername = (EditText)findViewById(R.id.txtUsername);
imageView = (ImageView)findViewById(R.id.imageView);
+ uploadButton = (Button)findViewById(R.id.btnUpload);
+
// create the output dir for us
File outputDirectory = new File(Environment.getExternalStorageDirectory(), OUTPUT_DIR);
@@ -91,7 +88,67 @@ public void takePicture(View view) {
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
+ public void doClear(View view) {
+ Log.i(TAG, "Clear");
+ if( imageReady ) {
+ imageReady = false;
+ ((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();
+ imageView.setImageBitmap(null);
+ }
+ }
+
+ public void doUpload(View view) {
+ Log.i(TAG, "Upload");
+ Long timeInMilliseconds = System.currentTimeMillis()/1000;
+ String timeInSeconds = timeInMilliseconds.toString();
+ MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
+
+ try {
+ // check for cookies
+ // create multipart data
+ File imageFile = new File(imageUri.getPath());
+ FileBody partFile = new FileBody(imageFile);
+ StringBody partTime = new StringBody(timeInSeconds);
+ multipartEntity.addPart("photo", partFile );
+ multipartEntity.addPart("device_timestamp", partTime);
+ } catch ( Exception e ) {
+ Log.e(TAG,"Error creating mulitpart form: " + e.toString());
+ Toast.makeText(MainActivity.this, "Create mulitpart failed " + e.toString(), Toast.LENGTH_LONG).show();
+ return;
+ }
+
+ // upload
+ try {
+ HttpPost httpPost = new HttpPost(UPLOAD_URL);
+ httpPost.setEntity(multipartEntity);
+ HttpResponse httpResponse = httpClient.execute(httpPost);
+ HttpEntity httpEntity = httpResponse.getEntity();
+ Log.i(TAG, "Upload response: " + httpResponse.getStatusLine());
+ Toast.makeText(MainActivity.this, httpResponse.getStatusLine().toString(), Toast.LENGTH_LONG).show();
+ } catch( Exception e ) {
+ Log.e(TAG, "HttpPost error: " + e.toString());
+ Toast.makeText(MainActivity.this, "Upload failed " + e.toString(), Toast.LENGTH_LONG).show();
+ }
+
+ // configure
+ try {
+ HttpPost httpPost = new HttpPost(CONFIGURE_URL);
+ List<NameValuePair> postParams = new ArrayList<NameValuePair>();
+ postParams.add(new BasicNameValuePair("device_timestamp", timeInSeconds));
+ postParams.add(new BasicNameValuePair("caption", TAG));
+ httpPost.setEntity(new UrlEncodedFormEntity(postParams, HTTP.UTF_8));
+ HttpResponse httpResponse = httpClient.execute(httpPost);
+ HttpEntity httpEntity = httpResponse.getEntity();
+ Log.i(TAG, "Configure response: " + httpResponse.getStatusLine());
+ Toast.makeText(MainActivity.this, httpResponse.getStatusLine().toString(), Toast.LENGTH_LONG).show();
+ } catch( Exception e ) {
+ Log.e(TAG, "HttpPost error: " + e.toString());
+ Toast.makeText(MainActivity.this, "Configure failed " + e.toString(), Toast.LENGTH_LONG).show();
+ }
+ }
+
public CookieStore doLogin(View view) {
+ StringBuffer cookieString = null;
Log.i(TAG, "Login button pressed");
String password = txtPassword.getText().toString();
String username = txtUsername.getText().toString();
@@ -108,22 +165,25 @@ public CookieStore doLogin(View view) {
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
- Log.i(TAG, "Login form get: " + httpResponse.getStatusLine());
+ Log.i(TAG, "Login response: " + httpResponse.getStatusLine());
Log.i(TAG, "Post logon cookies:");
cookieStore = httpClient.getCookieStore();
List<Cookie> cookieList = cookieStore.getCookies();
+ cookieString = new StringBuffer();
if (cookieList.isEmpty()) {
Log.i(TAG, "None");
} else {
for (int i = 0; i < cookieList.size(); i++) {
Log.i(TAG, "- " + cookieList.get(i).toString());
+ cookieString.append(cookieList.get(i).toString() + "\n");
}
- Toast.makeText(MainActivity.this, "Login successful", Toast.LENGTH_SHORT).show();
+ Toast.makeText(MainActivity.this, "Logged in" , Toast.LENGTH_SHORT).show();
+ if( imageReady ) uploadButton.setEnabled(true);
}
} catch( Exception e ) {
Log.e(TAG, "HttpPost error: " + e.toString());
- Toast.makeText(this, "doLogin failed " + e.toString(), Toast.LENGTH_LONG).show();
+ Toast.makeText(MainActivity.this, "doLogin failed " + e.toString(), Toast.LENGTH_LONG).show();
}
return cookieStore;
@@ -138,14 +198,26 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ContentResolver contentResolver = getContentResolver();
Bitmap imageBitmap;
try {
+ Drawable toRecycle = imageView.getDrawable();
+ if( toRecycle != null ) {
+ Bitmap bitmapToRecycle = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
+ if( bitmapToRecycle != null ) {
+ ((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();
+ }
+ }
imageBitmap = android.provider.MediaStore.Images.Media.getBitmap(contentResolver, imageUri);
imageView.setImageBitmap(imageBitmap);
- imageView.invalidate();
Log.i(TAG, "Image: " + imageUri.toString());
- Toast.makeText(MainActivity.this, imageUri.toString(), Toast.LENGTH_LONG).show();;
+ Toast.makeText(MainActivity.this, imageUri.toString(), Toast.LENGTH_LONG).show();
+ imageReady = true;
+
+ // turn on upload button
+ if( httpClient.getCookieStore() != null ) uploadButton.setEnabled(true);
} catch ( Exception e ) {
Toast.makeText(MainActivity.this, "Camera error", Toast.LENGTH_LONG).show();
Log.e(TAG, e.toString() );
+ imageReady = false;
+ imageUri = null;
}
}
}

0 comments on commit cf1f8a0

Please sign in to comment.