Skip to content

Commit

Permalink
File uploading to the server works!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
w9jds committed Dec 20, 2013
1 parent f46e2e3 commit 8d7a13a
Show file tree
Hide file tree
Showing 6 changed files with 491 additions and 175 deletions.
498 changes: 349 additions & 149 deletions .idea/workspace.xml

Large diffs are not rendered by default.

25 changes: 16 additions & 9 deletions GlassShare/GlassShare-GlassShare.iml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<component name="FacetManager">
<facet type="android" name="Android">
<configuration>
<option name="SELECTED_BUILD_VARIANT" value="DefaultFlavorDebug" />
<option name="SELECTED_BUILD_VARIANT" value="defaultFlavorDebug" />
<option name="ASSEMBLE_TASK_NAME" value="assembleDefaultFlavorDebug" />
<option name="COMPILE_JAVA_TASK_NAME" value="compileDefaultFlavorDebug" />
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDefaultFlavorTest" />
<option name="COMPILE_JAVA_TASK_NAME" value="compileDefaultFlavorDebugJava" />
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDefaultFlavorDebugTest" />
<option name="SOURCE_GEN_TASK_NAME" value="generateDefaultFlavorDebugSources" />
<option name="ALLOW_USER_CONFIGURATION" value="false" />
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
Expand All @@ -27,14 +27,21 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/build/source/r/defaultFlavor/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/aidl/defaultFlavor/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/rs/defaultFlavor/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/buildConfig/defaultFlavor/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/rs/defaultFlavor/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/res/rs/defaultFlavor/debug" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/build/source/r/defaultFlavor/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/aidl/defaultFlavor/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/rs/defaultFlavor/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/buildConfig/defaultFlavor/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/build/res/rs/defaultFlavor/test" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/defaultFlavorDebug/aidl" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/defaultFlavorDebug/assets" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/defaultFlavorDebug/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/defaultFlavorDebug/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/defaultFlavorDebug/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/defaultFlavorDebug/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/defaultFlavorDebug/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/build/source/r/test/defaultFlavor/debug" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/aidl/test/defaultFlavor/debug" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/buildConfig/test/defaultFlavor/debug" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/rs/test/defaultFlavor/debug" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/build/res/rs/test/defaultFlavor/debug" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/defaultFlavor/aidl" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/defaultFlavor/assets" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/defaultFlavor/java" isTestSource="false" />
Expand Down
2 changes: 1 addition & 1 deletion GlassShare/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
classpath 'com.android.tools.build:gradle:0.7.+'
classpath 'com.newrelic.agent.android:agent-gradle-plugin:3.239.0'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public StorageService(Context context)
try
{

// mTableTables = mClient.getTable("");
// mTableTableRows = mClient.getTable("");
mTableContainers = mClient.getTable("blobcontainers");
mTableBlobs = mClient.getTable("blobs");
}
Expand Down Expand Up @@ -454,7 +452,8 @@ public void onCompleted(JsonElement result, int count, Exception exception, Serv
* @param containerName
* @param blobName
*/
public void deleteBlob(final String containerName, String blobName) {
public void deleteBlob(final String containerName, String blobName)
{
//Create the json Object we'll send over and fill it with the required
//id property - otherwise we'll get kicked back
JsonObject blob = new JsonObject();
Expand Down
132 changes: 121 additions & 11 deletions GlassShare/src/main/java/com/w9jds/glassshare/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
import android.accounts.AccountManager;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
Expand All @@ -25,13 +30,16 @@
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
import com.microsoft.windowsazure.mobileservices.MobileServiceClient;
import com.newrelic.agent.android.NewRelic;
import com.google.gson.JsonObject;
import com.w9jds.glassshare.Adapters.csaAdapter;
import com.w9jds.glassshare.Classes.StorageApplication;
import com.w9jds.glassshare.Classes.StorageService;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
Expand All @@ -43,9 +51,9 @@ public class MainActivity extends Activity
private final String CAMERA_IMAGE_BUCKET_ID = getBucketId(CAMERA_IMAGE_BUCKET_NAME);

private ConnectivityManager mcmCon;
private Uri mImageUri;

//create member variables for Azure
private MobileServiceClient mClient;
private StorageService mStorageService;

//create member variables for google drive
Expand Down Expand Up @@ -75,7 +83,7 @@ protected void onCreate(Bundle savedInstanceState)
}

//get all the images from the camera folder (paths)
mlsPaths = getCameraImages(this);
mlsPaths = getCameraImages();
//sort the paths of pictures
sortPaths();
//create a new card scroll viewer for this context
Expand Down Expand Up @@ -104,6 +112,29 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)

}

/***
* Register for broadcasts
*/
@Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction("blob.created");
registerReceiver(receiver, filter);
super.onResume();
}

/***
* Unregister for broadcasts
*/
@Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}

/***
* Sort the file paths so that the images are in order from most resent first
*/
private void sortPaths()
{
java.io.File[] fPics = new java.io.File[mlsPaths.size()];
Expand Down Expand Up @@ -131,12 +162,16 @@ public String getBucketId(String path)
return String.valueOf(path.toLowerCase().hashCode());
}

public ArrayList<String> getCameraImages(Context context)
/***
* Get all the image file paths on this device (from the camera folder)
* @return an arraylist of all the file paths
*/
public ArrayList<String> getCameraImages()
{
final String[] projection = {MediaStore.Images.Media.DATA};
final String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
final String[] selectionArgs = { CAMERA_IMAGE_BUCKET_ID };
final Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null);
final Cursor cursor = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null);
ArrayList<String> result = new ArrayList<String>(cursor.getCount());

if (cursor.moveToFirst())
Expand Down Expand Up @@ -212,31 +247,106 @@ public boolean onOptionsItemSelected(android.view.MenuItem iItem)
{

String sContainer = "";
String[] saImage = mlsPaths.get(iPosition).split("/|\\.");

Account[] myAccounts = AccountManager.get(this).getAccounts();
//for each account
for (int i = 0; i < myAccounts.length; i++)
{
//if the account type is google
if (myAccounts[i].type.equals("com.google"))
{
//set this as the selected Account
sContainer = myAccounts[i].name;
String[] saAccount = myAccounts[i].name.split("@|\\.");
sContainer = saAccount[0] + saAccount[1] + saAccount[2];
}
}

// mStorageService.getTables();

mStorageService.addContainer(sContainer, false);
// mStorageService.getSasForNewBlob(sContainer, "testimage");
mStorageService.getSasForNewBlob(sContainer, saImage[saImage.length-2]);

}

return true;

default:
return super.onOptionsItemSelected(iItem);
}
}

/***
* Broadcast receiver handles blobs being loaded or a new blob being created
*/
private BroadcastReceiver receiver = new BroadcastReceiver()
{
public void onReceive(Context context, android.content.Intent intent)
{
String intentAction = intent.getAction();

if (intentAction.equals("blob.created"))
{
//If a blob has been created, upload the image
JsonObject blob = mStorageService.getLoadedBlob();
String sasUrl = blob.getAsJsonPrimitive("sasUrl").toString();
(new ImageUploaderTask(sasUrl)).execute();

}
}
};

/***
* Handles uploading an image to a specified url
*/
class ImageUploaderTask extends AsyncTask<Void, Void, Boolean> {
private String mUrl;
public ImageUploaderTask(String url) {
mUrl = url;
}

@Override
protected Boolean doInBackground(Void... params) {
try
{
//get the image data
Bitmap bmp = BitmapFactory.decodeFile(mlsPaths.get(iPosition));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// Post our image data (byte array) to the server
URL url = new URL(mUrl.replace("\"", ""));
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("PUT");
urlConnection.addRequestProperty("Content-Type", "image/jpeg");
urlConnection.setRequestProperty("Content-Length", ""+ byteArray.length);
// Write image data to server
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
wr.write(byteArray);
wr.flush();
wr.close();
int response = urlConnection.getResponseCode();
//If we successfully uploaded, return true
if (response == 201 && urlConnection.getResponseMessage().equals("Created"))
return true;
}

catch (Exception ex)
{
Log.e("GlassShareImageUploadTask", ex.getMessage());
}
return false;
}

@Override
protected void onPostExecute(Boolean uploaded)
{
// if (uploaded)
// {
// mAlertDialog.cancel();
// mStorageService.getBlobsForContainer(mContainerName);
// }
}
}

private void saveFileToDrive(String sPath)
{
final String msPath = sPath;
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Dec 13 23:50:43 CST 2013
#Wed Dec 18 23:19:43 CST 2013
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.8-all.zip
distributionUrl=http\://services.gradle.org/distributions/gradle-1.9-all.zip

0 comments on commit 8d7a13a

Please sign in to comment.