lose SD card dependency, fix NPEs

// FREEBIE
This commit is contained in:
Jake McGinty 2014-02-23 23:30:43 -08:00
parent 86b3de2a93
commit f30304423d
3 changed files with 21 additions and 21 deletions

View File

@ -9,7 +9,6 @@ import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
@ -81,7 +80,7 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv
private final DynamicTheme dynamicTheme = new DynamicTheme(); private final DynamicTheme dynamicTheme = new DynamicTheme();
private final DynamicLanguage dynamicLanguage = new DynamicLanguage(); 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_CONTACT = 1;
private static final int PICK_AVATAR = 2; private static final int PICK_AVATAR = 2;
@ -266,7 +265,7 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv
photoPickerIntent.putExtra("aspectY", 1); photoPickerIntent.putExtra("aspectY", 1);
photoPickerIntent.putExtra("outputX", AVATAR_SIZE); photoPickerIntent.putExtra("outputX", AVATAR_SIZE);
photoPickerIntent.putExtra("outputY", 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()); photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
startActivityForResult(photoPickerIntent, PICK_AVATAR); startActivityForResult(photoPickerIntent, PICK_AVATAR);
} }
@ -275,23 +274,20 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv
((RecipientsEditor)findViewById(R.id.recipients_text)).setHint(R.string.recipients_panel__add_member); ((RecipientsEditor)findViewById(R.id.recipients_text)).setHint(R.string.recipients_panel__add_member);
} }
private Uri getTempUri() { private Uri getAvatarTempUri() {
return Uri.fromFile(getTempFile()); return Uri.fromFile(createAvatarTempFile());
} }
private File getTempFile() { private File createAvatarTempFile() {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File f = new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE);
try { try {
f.createNewFile(); File f = File.createTempFile("avatar", ".tmp", getFilesDir());
pendingFile = f;
f.setWritable(true, false);
f.deleteOnExit(); 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();
}
return f; 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; return null;
} }
} }
@ -517,9 +513,11 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv
@Override @Override
protected Bitmap doInBackground(Void... voids) { protected Bitmap doInBackground(Void... voids) {
File tempFile = getTempFile(); if (pendingFile != null) {
avatarBmp = BitmapUtil.getCircleCroppedBitmap(BitmapFactory.decodeFile(tempFile.getAbsolutePath())); avatarBmp = BitmapUtil.getCircleCroppedBitmap(BitmapFactory.decodeFile(pendingFile.getAbsolutePath()));
tempFile.delete(); pendingFile.delete();
pendingFile = null;
}
return avatarBmp; return avatarBmp;
} }

View File

@ -161,7 +161,8 @@ public class GroupDatabase extends Database {
public void updateAvatar(byte[] groupId, byte[] avatar) { public void updateAvatar(byte[] groupId, byte[] avatar) {
updateAvatarInDatabase(groupId, 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) { private void updateAvatarInDatabase(byte[] groupId, byte[] avatar) {

View File

@ -105,6 +105,7 @@ public class BitmapUtil {
} }
public static Bitmap getCircleCroppedBitmap(Bitmap bitmap) { public static Bitmap getCircleCroppedBitmap(Bitmap bitmap) {
if (bitmap == null) return null;
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888); bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output); Canvas canvas = new Canvas(output);