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

Allowed the application to save picture in background. #4

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
3 changes: 2 additions & 1 deletion starter-code/.idea/gradle.xml

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

16 changes: 16 additions & 0 deletions 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 @@ -20,6 +21,7 @@ public class MainActivity extends AppCompatActivity{
private static final int PICK_IMAGE_REQUEST = 1;
private ImageView image;
private Button change;
private Uri mSelectedImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -42,24 +44,10 @@ 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();
}

//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();
mSelectedImage = data.getData();
image.setImageURI(mSelectedImage);
android.os.AsyncTask AsyncTask = new AsyncTask();
AsyncTask.execute();
}
}

Expand All @@ -81,4 +69,38 @@ private void changeProfileImage() {
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
private class AsyncTask extends android.os.AsyncTask<Object,Void,Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected Void doInBackground(Object... params) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(mSelectedImage));
} 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);
Toast.makeText(MainActivity.this,"The image is set as a profile picture", Toast.LENGTH_LONG).show();
}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}