Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Created September 24, 2012 19:08
Show Gist options
  • Save ChrisRisner/3777705 to your computer and use it in GitHub Desktop.
Save ChrisRisner/3777705 to your computer and use it in GitHub Desktop.
MobileServices and Android 2
private Button mBtnSaveTodo;
private Button mBtnPickImage;
private Button mBtnMarkTodoComplete;
private TextView mLblTodoText;
private EditText mTxtTodoText;
private boolean mIsAddingNewTodo;
private String mTodoText;
private int mTodoId;
private ImageView mImageView;
private Uri mImageUrl;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/lblTodoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:text="ToDo Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/txtTodoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/lblTodoText"
android:layout_marginTop="26dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSaveTodo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txtTodoText"
android:text="Save ToDo" />
<Button
android:id="@+id/btnPickImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnSaveTodo"
android:text="Pick Image" />
<Button
android:id="@+id/btnMarkTodoComplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txtTodoText"
android:text="Mark ToDo Complete" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
mBtnPickImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pickImage();
}
});
protected void pickImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 987);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
//handle result from gallary select
if (requestCode == 987) {
Uri currImageURI = data.getData();
this.mImageUrl = currImageURI;
//Set the image view's image by using imageUri
mImageView.setImageURI(currImageURI);
}
} catch (Exception ex) {
Log.e("TodoDetailsActivity", "Error in onActivityResult: " + ex.getMessage());
}
}
@Override
protected String doInBackground(String... params) {
String todoText = params[0];
String imageData = null;
if (params.length > 1)
imageData = params[1];
JSONObject jsonUrl = new JSONObject();
try {
jsonUrl.put("complete", "false");
jsonUrl.put("text", todoText);
if (imageData != null)
jsonUrl.put("coltest", imageData);
} catch (JSONException e) {
Log.e("TodoDetailsActivity",
"Error creating JSON object: " + e.getMessage());
}
protected void saveTodo() {
String imageString = null;
if (mImageUrl != null && !mImageUrl.equals("")) {
try {
Cursor cursor = getContentResolver().query(mImageUrl, null,
null, null, null);
cursor.moveToFirst();
int index = cursor
.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
String absoluteFilePath = cursor.getString(index);
FileInputStream fis = new FileInputStream(absoluteFilePath);
int bytesRead = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
while ((bytesRead = fis.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byte[] bytes = bos.toByteArray();
imageString = Base64.encodeToString(bytes, Base64.DEFAULT);
} catch (Exception ex) {
ex.printStackTrace();
}
}
new SaveTodoTask(this).execute(mTxtTodoText.getText().toString(), imageString);
}
if (mIsAddingNewTodo) {
mBtnMarkTodoComplete.setVisibility(View.GONE);
} else {
mBtnSaveTodo.setVisibility(View.GONE);
mBtnPickImage.setVisibility(View.GONE);
mTodoText = intent.getStringExtra("TodoText");
mTodoId = intent.getIntExtra("TodoId", 0);
mTxtTodoText.setText(mTodoText);
mTxtTodoText.setFocusable(false);
String imageString = intent.getStringExtra("image");
if (imageString != null && !imageString.equals("")) {
byte[] imageAsBytes = Base64.decode(imageString, Base64.DEFAULT);
mImageView.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo_details);
// Get references to controls on layout
mBtnSaveTodo = (Button) findViewById(R.id.btnSaveTodo);
mBtnPickImage = (Button) findViewById(R.id.btnPickImage);
mBtnMarkTodoComplete = (Button) findViewById(R.id.btnMarkTodoComplete);
mLblTodoText = (TextView) findViewById(R.id.lblTodoText);
mTxtTodoText = (EditText) findViewById(R.id.txtTodoText);
mImageView = (ImageView) findViewById(R.id.imageView);
// Get extra data from intent
Intent intent = getIntent();
mIsAddingNewTodo = intent.getBooleanExtra("AddingNewTodo", false);
if (mIsAddingNewTodo) {
mBtnMarkTodoComplete.setVisibility(View.GONE);
} else {
mBtnSaveTodo.setVisibility(View.GONE);
mBtnPickImage.setVisibility(View.GONE);
try {
for (JSONObject todoItem : mTodos) {
if (todoItem.getString("text").equals(
tv.getText().toString())) {
todoDetailsIntent.putExtra("TodoId", todoItem.getInt("id"));
todoDetailsIntent.putExtra("image", todoItem.getString("coltest"));
}
}
} catch (Exception ex) {
Log.e("TodoListActivity", ex.getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment