Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mike Kwon thread lab + bonus #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions starter-code/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions starter-code/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion starter-code/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
Expand All @@ -18,8 +19,9 @@
public class MainActivity extends AppCompatActivity{
private static final String TAG = "makeappthreadsafe";
private static final int PICK_IMAGE_REQUEST = 1;
private ImageView image;
private ImageView image, previousImage;
private Button change;
private Uri selectedImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -28,6 +30,7 @@ protected void onCreate(Bundle savedInstanceState) {

change = (Button) findViewById(R.id.choose_button);
image = (ImageView) findViewById(R.id.image);
previousImage = (ImageView) findViewById(R.id.previousImage);
setProfileImage();
change.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -42,24 +45,17 @@ public void onClick(View v) {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == MainActivity.RESULT_OK && null != data) {
Uri selectedImage = data.getData();
image.setImageURI(selectedImage);

//saves a new picture to a file
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage));
} catch (FileNotFoundException e) {
Log.d(TAG, "Image uri is not received or recognized");
}
try {
PictureUtil.saveToCacheFile(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
//Save previous image to another thumbnail
previousImage.setImageDrawable(image.getDrawable());

//provides a feedback that the image is set as a profile picture
Toast.makeText(this, "The image is set as a profile picture", Toast.LENGTH_LONG).show();
//pull new image data and set imageView with new image
selectedImage = data.getData();
image.setImageURI(selectedImage);

//start AsyncTask to save image
Task task = new Task();
task.execute();
}
}

Expand All @@ -81,4 +77,31 @@ private void changeProfileImage() {
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

private class Task extends AsyncTask<Void,Void,Void>{

@Override
protected Void doInBackground(Void... voids) {
//saves a new picture to a file
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage));
} catch (FileNotFoundException e) {
Log.d(TAG, "Image uri is not received or recognized");
}
try {
PictureUtil.saveToCacheFile(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//provides feedback that the image is set as a profile picture
Toast.makeText(MainActivity.this, "The image is set as a profile picture", Toast.LENGTH_LONG).show();
}
}
}
7 changes: 7 additions & 0 deletions starter-code/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@
android:text="@string/choose"
android:layout_below="@+id/image"/>

<ImageView
android:id="@+id/previousImage"
android:layout_height="80dp"
android:layout_width="80dp"
android:layout_below="@id/choose_button"
android:contentDescription="this was your previous profile image"/>

</RelativeLayout>