Skip to content

Commit

Permalink
Uploading images for annotations works now
Browse files Browse the repository at this point in the history
  • Loading branch information
ftyau committed Nov 25, 2013
1 parent 7dfa989 commit 381df9e
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 35 deletions.
14 changes: 13 additions & 1 deletion res/layout/activity_edit_create_annot.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@
android:layout_height="wrap_content"
android:text="@string/save"
android:onClick="saveAnnot"/>

<Button
android:id="@+id/upload_image_annot"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Upload image from memory"
android:onClick="uploadImage"/>
<Button
android:id="@+id/camera_annot"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Upload image from camera"
android:onClick="uploadCamera"/>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
Expand Down
4 changes: 2 additions & 2 deletions src/com/example/team04adventure/Model/AnnotationAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public View getView(int position, View convertView, ViewGroup parent) {
holder = (ViewHolder) convertView.getTag();

if (annots.get(position).getImage() == null) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.defaultprofile);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 250, 250, false);
holder.image.setImageBitmap(scaledBitmap);
} else {
String encodedString = annots.get(position).getImage();
Expand Down
25 changes: 25 additions & 0 deletions src/com/example/team04adventure/Model/Media.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
public class Media {

private static final int IMAGE_MAX = 250;

long id;
String content;
String type;
Expand Down Expand Up @@ -115,6 +117,29 @@ public static Bitmap decodeBase64(String bArray) {
byte[] decodedByte = Base64.decode(bArray, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

public static Bitmap resizeImage(Bitmap bitmap) {
float width = bitmap.getWidth();
float height = bitmap.getHeight();
float scale = 1;
if (width >= height) {
scale = IMAGE_MAX / width;
} else if (width < height) {
scale = IMAGE_MAX / height;
}
Bitmap resizedBitmap = null;
if (scale <= 1) {
float newWidth = width * scale;
float newHeight = height * scale;
int newWidthInt = (int) newWidth;
int newHeightInt = (int) newHeight;
resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidthInt,
newHeightInt, false);
} else {
resizedBitmap = bitmap;
}
return resizedBitmap;
}


}
89 changes: 85 additions & 4 deletions src/com/example/team04adventure/View/EditCreateAnnot.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@

package com.example.team04adventure.View;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
Expand All @@ -28,11 +37,15 @@
import com.example.team04adventure.Model.Annotation;
import com.example.team04adventure.Model.Frag;
import com.example.team04adventure.Model.JSONparser;
import com.example.team04adventure.Model.Media;
import com.example.team04adventure.Model.StorageManager;
import com.example.team04adventure.Model.Story;

public class EditCreateAnnot extends Activity {

private static final int SELECT_PICTURE = 1;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

String sid,
fid;
String online;
Expand All @@ -46,6 +59,10 @@ public class EditCreateAnnot extends Activity {
EditText reviewIn;
ProgressDialog mDialog;
Button saveButton;
Annotation a;

private Uri imageFileUri;


@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -61,6 +78,8 @@ protected void onCreate(Bundle savedInstanceState) {
reviewIn = (EditText) findViewById(R.id.annotBody);
saveButton = (Button) findViewById(R.id.save);

a = new Annotation();

}

@Override
Expand All @@ -77,12 +96,9 @@ public void saveAnnot(View view) {
mDialog.show();
review = reviewIn.getText().toString();
author = MainActivity.username;

Annotation a = new Annotation();


a.setAuthor(author);
a.setReview(review);
a.setImage(image);

if (online.equals("online")) {
Integer index = Integer.valueOf(-2);
Expand Down Expand Up @@ -133,6 +149,71 @@ public void saveAnnot(View view) {

finish();
}

public void uploadImage(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}

public void uploadCamera(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

String folder = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/team04adventure/pic";
File folderF = new File(folder);
if (!folderF.exists()) {
folderF.mkdir();
}

String imageFilePath = folder + "/"
+ String.valueOf(System.currentTimeMillis()) + ".jpg";
File imageFile = new File(imageFilePath);
imageFileUri = Uri.fromFile(imageFile);

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE
&& resultCode == RESULT_OK) {
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), imageFileUri);
Bitmap resizedBitmap = Media.resizeImage(bitmap);
String convertedString = Media.encodeToBase64(resizedBitmap);
a.setImage(convertedString);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), selectedImageUri);
Bitmap resizedBitmap = Media.resizeImage(bitmap);
String convertedString = Media.encodeToBase64(resizedBitmap);
a.setImage(convertedString);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void onStop(){
super.onStop();

Expand Down
31 changes: 3 additions & 28 deletions src/com/example/team04adventure/View/EditFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public class EditFragment extends Activity {
private static final int SELECT_PICTURE = 1;
private static final int SELECT_PROFILE = 101;

private static final int IMAGE_MAX = 250;

private Button uploadButton;
private Button cameraButton;
private Button profileButton;
Expand Down Expand Up @@ -207,7 +205,7 @@ protected void onActivityResult(int requestCode, int resultCode,
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), imageFileUri);
Bitmap resizedBitmap = resizeImage(bitmap);
Bitmap resizedBitmap = Media.resizeImage(bitmap);

uploadTextView.setText("Image you just added:");
uploadImageView.setImageBitmap(resizedBitmap);
Expand All @@ -233,7 +231,7 @@ protected void onActivityResult(int requestCode, int resultCode,
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), selectedImageUri);
Bitmap resizedBitmap = resizeImage(bitmap);
Bitmap resizedBitmap = Media.resizeImage(bitmap);

uploadTextView.setText("Image you just added:");
uploadImageView.setImageBitmap(resizedBitmap);
Expand All @@ -257,7 +255,7 @@ protected void onActivityResult(int requestCode, int resultCode,
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), selectedImageUri);
Bitmap resizedBitmap = resizeImage(bitmap);
Bitmap resizedBitmap = Media.resizeImage(bitmap);

illustrationTextView.setText("Profile picture you just set:");
illustrationImageView.setImageBitmap(resizedBitmap);
Expand Down Expand Up @@ -418,29 +416,6 @@ public void saveFrag() {

}

public Bitmap resizeImage(Bitmap bitmap) {
float width = bitmap.getWidth();
float height = bitmap.getHeight();
float scale = 1;
if (width >= height) {
scale = IMAGE_MAX / width;
} else if (width < height) {
scale = IMAGE_MAX / height;
}
Bitmap resizedBitmap = null;
if (scale <= 1) {
float newWidth = width * scale;
float newHeight = height * scale;
int newWidthInt = (int) newWidth;
int newHeightInt = (int) newHeight;
resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidthInt,
newHeightInt, false);
} else {
resizedBitmap = bitmap;
}
return resizedBitmap;
}

public void fragSaveText() {
String fragTitleString = fragTitle.getText().toString();
String fragBodyString = fragBody.getText().toString();
Expand Down

0 comments on commit 381df9e

Please sign in to comment.