Skip to content

Commit

Permalink
lose SD card dependency, fix NPEs
Browse files Browse the repository at this point in the history
// FREEBIE
  • Loading branch information
mcginty committed Feb 24, 2014
1 parent 86b3de2 commit f303044
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
38 changes: 18 additions & 20 deletions src/org/thoughtcrime/securesms/GroupCreateActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextUtils;
Expand Down Expand Up @@ -81,7 +80,7 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv
private final DynamicTheme dynamicTheme = new DynamicTheme();
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();

private static final String TEMP_PHOTO_FILE = "__tmp_group_create_avatar_photo.tmp";
private File pendingFile = null;

private static final int PICK_CONTACT = 1;
private static final int PICK_AVATAR = 2;
Expand Down Expand Up @@ -266,7 +265,7 @@ public void onClick(View view) {
photoPickerIntent.putExtra("aspectY", 1);
photoPickerIntent.putExtra("outputX", AVATAR_SIZE);
photoPickerIntent.putExtra("outputY", AVATAR_SIZE);
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getAvatarTempUri());
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
startActivityForResult(photoPickerIntent, PICK_AVATAR);
}
Expand All @@ -275,23 +274,20 @@ public void onClick(View view) {
((RecipientsEditor)findViewById(R.id.recipients_text)).setHint(R.string.recipients_panel__add_member);
}

private Uri getTempUri() {
return Uri.fromFile(getTempFile());
private Uri getAvatarTempUri() {
return Uri.fromFile(createAvatarTempFile());
}

private File getTempFile() {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {

File f = new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE);
try {
f.createNewFile();
f.deleteOnExit();
} catch (IOException e) {
Log.e(TAG, "Error creating new temp file.", e);
Toast.makeText(getApplicationContext(), R.string.GroupCreateActivity_file_io_exception, Toast.LENGTH_SHORT).show();
}
private File createAvatarTempFile() {
try {
File f = File.createTempFile("avatar", ".tmp", getFilesDir());
pendingFile = f;
f.setWritable(true, false);
f.deleteOnExit();
return f;
} else {
} catch (IOException ioe) {
Log.e(TAG, "Error creating new temp file.", ioe);
Toast.makeText(getApplicationContext(), R.string.GroupCreateActivity_file_io_exception, Toast.LENGTH_SHORT).show();
return null;
}
}
Expand Down Expand Up @@ -517,9 +513,11 @@ private class DecodeCropAndSetAsyncTask extends AsyncTask<Void,Void,Bitmap> {

@Override
protected Bitmap doInBackground(Void... voids) {
File tempFile = getTempFile();
avatarBmp = BitmapUtil.getCircleCroppedBitmap(BitmapFactory.decodeFile(tempFile.getAbsolutePath()));
tempFile.delete();
if (pendingFile != null) {
avatarBmp = BitmapUtil.getCircleCroppedBitmap(BitmapFactory.decodeFile(pendingFile.getAbsolutePath()));
pendingFile.delete();
pendingFile = null;
}
return avatarBmp;
}

Expand Down
3 changes: 2 additions & 1 deletion src/org/thoughtcrime/securesms/database/GroupDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ public void updateAvatar(byte[] groupId, Bitmap avatar) {

public void updateAvatar(byte[] groupId, byte[] avatar) {
updateAvatarInDatabase(groupId, avatar);
updateGroupRecipientAvatar(groupId, BitmapFactory.decodeByteArray(avatar, 0, avatar.length));
Bitmap bitmap = (avatar == null ? null : BitmapFactory.decodeByteArray(avatar, 0, avatar.length));
updateGroupRecipientAvatar(groupId, bitmap);
}

private void updateAvatarInDatabase(byte[] groupId, byte[] avatar) {
Expand Down
1 change: 1 addition & 0 deletions src/org/thoughtcrime/securesms/util/BitmapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ private static BitmapFactory.Options getImageDimensions(InputStream inputStream)
}

public static Bitmap getCircleCroppedBitmap(Bitmap bitmap) {
if (bitmap == null) return null;
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Expand Down

0 comments on commit f303044

Please sign in to comment.