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

Marat Mamin - Profile Picture Change #1

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
43 changes: 43 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 @@ -14,6 +15,7 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;

public class MainActivity extends AppCompatActivity{
private static final String TAG = "makeappthreadsafe";
Expand Down Expand Up @@ -45,21 +47,24 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri selectedImage = data.getData();
image.setImageURI(selectedImage);

tryAsyncTask task = new tryAsyncTask();
task.execute(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();
}
// 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();
// Toast.makeText(this, "The image is set as a profile picture", Toast.LENGTH_LONG).show();
}
}

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

private class tryAsyncTask extends AsyncTask<Uri, Void, Void> {

@Override
protected Void doInBackground(Uri... params) {


Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(params[0]));
} 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 onPreExecute() {
super.onPreExecute();
}

@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);
}
}
}