diff --git a/starter-code/.idea/misc.xml b/starter-code/.idea/misc.xml index 5d19981..7158618 100644 --- a/starter-code/.idea/misc.xml +++ b/starter-code/.idea/misc.xml @@ -43,4 +43,20 @@ + + + + + 1.8 + + + + + + + \ No newline at end of file diff --git a/starter-code/app/src/main/java/generalassembly/yuliyakaleda/makeappthreadsafe/MainActivity.java b/starter-code/app/src/main/java/generalassembly/yuliyakaleda/makeappthreadsafe/MainActivity.java index ece7515..70a690c 100644 --- a/starter-code/app/src/main/java/generalassembly/yuliyakaleda/makeappthreadsafe/MainActivity.java +++ b/starter-code/app/src/main/java/generalassembly/yuliyakaleda/makeappthreadsafe/MainActivity.java @@ -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; @@ -15,70 +16,86 @@ import java.io.FileNotFoundException; import java.io.IOException; -public class MainActivity extends AppCompatActivity{ - private static final String TAG = "makeappthreadsafe"; - private static final int PICK_IMAGE_REQUEST = 1; - private ImageView image; - private Button change; +public class MainActivity extends AppCompatActivity { + private static final String TAG = "makeappthreadsafe"; + private static final int PICK_IMAGE_REQUEST = 1; + private ImageView image; + private Button change; + private Uri selectedImage; - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); - change = (Button) findViewById(R.id.choose_button); - image = (ImageView) findViewById(R.id.image); - setProfileImage(); - change.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - changeProfileImage(); - } - }); - } + change = (Button) findViewById(R.id.choose_button); + image = (ImageView) findViewById(R.id.image); + setProfileImage(); + change.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + changeProfileImage(); + } + }); + } + + private class SleeperAsyncTask extends AsyncTask { + + @Override + protected Void doInBackground(Void... params) { + //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; + } - // sets the chosen image as a profile picture - @Override - 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); + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + //provides a 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(); + } + } - //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(); - } + // sets the chosen image as a profile picture + @Override + 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) { + selectedImage = data.getData(); + image.setImageURI(selectedImage); + SleeperAsyncTask task = new SleeperAsyncTask(); + task.execute(); + } + } - //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(); + //sets the image view of the profile picture to the previously saved image or the placeholder if + // the image has never been modified + private void setProfileImage() { + Bitmap bm = PictureUtil.loadFromCacheFile(); + if (bm != null) { + image.setImageBitmap(bm); + } else { + image.setImageResource(R.drawable.placeholder); + } } - } - //sets the image view of the profile picture to the previously saved image or the placeholder if - // the image has never been modified - private void setProfileImage() { - Bitmap bm = PictureUtil.loadFromCacheFile(); - if (bm != null) { - image.setImageBitmap(bm); - } else { - image.setImageResource(R.drawable.placeholder); + // brings up the photo gallery/other resources to choose a picture + private void changeProfileImage() { + Intent intent = new Intent(); + intent.setType("image/*"); + intent.setAction(Intent.ACTION_GET_CONTENT); + startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); } - } - // brings up the photo gallery/other resources to choose a picture - private void changeProfileImage() { - Intent intent = new Intent(); - intent.setType("image/*"); - intent.setAction(Intent.ACTION_GET_CONTENT); - startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); - } + }