Created
September 5, 2017 22:38
-
-
Save dirkvranckaert/501707fb107dd5335f98661efd008d73 to your computer and use it in GitHub Desktop.
Fixing the profile image selection for Samsung devices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package be.uest.terva.activity; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Matrix; | |
import android.media.ExifInterface; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.provider.MediaStore; | |
import android.support.annotation.Nullable; | |
import android.support.v4.content.FileProvider; | |
import android.widget.Button; | |
import com.squareup.picasso.Picasso; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import be.uest.terva.R; | |
public class ProfileImageActivity extends Activity { | |
private static final int REQUEST_CODE_CAMERA = 1; | |
private File tempProfileImageFile; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// set some content view... | |
Button button = findViewById(R.id.button); | |
button.setOnClickListener(view -> startCamera()); | |
} | |
@Override | |
protected void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
outState.putSerializable("tempProfileFile", tempProfileFile); | |
} | |
@Override | |
protected void onRestoreInstanceState(Bundle savedInstanceState) { | |
super.onRestoreInstanceState(savedInstanceState); | |
tempProfileFile = (File) savedInstanceState.getSerializable("tempProfileFile"); | |
} | |
private void startCamera() { | |
File tempFile = createTempImageFile(); | |
if (tempFile != null) { | |
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
if ((intent.resolveActivity(getPackageManager()) != null)) { | |
tempProfileImageFile = tempFile; | |
Uri photoURI = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".profileimage.fileprovider", tempFile); | |
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); | |
startActivityForResult(intent, REQUEST_CODE_CAMERA); | |
} | |
} | |
} | |
private File createTempImageFile() { | |
try { | |
return File.createTempFile( | |
"TEMP_PROFILE_IMAGE", | |
".jpg", | |
getExternalFilesDir("images") | |
); | |
} catch (IOException e) { | |
return null; | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) { | |
saveProfilePicture(tempProfileImageFile); | |
} | |
} | |
private void saveProfilePicture(File tempFile) { | |
final File imageFile = new File(getExternalFilesDir("images"), "my_profile_picture.jpg");; | |
if (imageFile.exists()) { | |
imageFile.delete(); | |
} | |
String tempPath = tempFile.getAbsolutePath(); | |
try { | |
Bitmap bitmapOrg = createOriginalBitmap(tempPath); | |
bitmapOrg = rotateImage(tempPath, bitmapOrg); | |
final Bitmap finalBitmap = resizeBitmap(bitmapOrg); | |
FileOutputStream out = null; | |
try { | |
out = new FileOutputStream(imageFile); | |
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); | |
} catch (final Exception e) { | |
} finally { | |
try { | |
if (out != null) { | |
out.close(); | |
} | |
} catch (final IOException e) { | |
} | |
} | |
} catch (final Exception e) { | |
return; | |
} | |
Picasso.with(this).invalidate(imageFile); | |
try { | |
tempFile.delete(); | |
} catch (final Exception e) { | |
} | |
} | |
private Bitmap createOriginalBitmap(final String imagePath) { | |
final Bitmap bitmapOrg; | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
bitmapOrg = BitmapFactory.decodeFile(imagePath); | |
} else { | |
final BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = false; | |
options.inPreferredConfig = Bitmap.Config.RGB_565; | |
options.inDither = true; | |
bitmapOrg = BitmapFactory.decodeFile(imagePath, options); | |
} | |
return bitmapOrg; | |
} | |
private static Bitmap rotateImage(final String imagePath, Bitmap source) throws IOException { | |
final ExifInterface ei = new ExifInterface(imagePath); | |
final int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); | |
switch (orientation) { | |
case ExifInterface.ORIENTATION_ROTATE_90: | |
source = rotateImageByAngle(source, 90); | |
break; | |
case ExifInterface.ORIENTATION_ROTATE_180: | |
source = rotateImageByAngle(source, 180); | |
break; | |
case ExifInterface.ORIENTATION_ROTATE_270: | |
source = rotateImageByAngle(source, 270); | |
break; | |
} | |
return source; | |
} | |
public static Bitmap rotateImageByAngle(final Bitmap source, final float angle) { | |
final Matrix matrix = new Matrix(); | |
matrix.postRotate(angle); | |
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); | |
} | |
private static Bitmap resizeBitmap(Bitmap source) { | |
final int heightOrg = source.getHeight(); | |
final int heightNew = 800; | |
if (heightNew < heightOrg) { | |
final int widthOrg = source.getWidth(); | |
final int widthNew = (heightNew * widthOrg) / heightOrg; | |
final Matrix matrix = new Matrix(); | |
matrix.postScale(((float) widthNew) / widthOrg, ((float) heightNew) / heightOrg); | |
source = Bitmap.createBitmap(source, 0, 0, widthOrg, heightOrg, matrix, false); | |
} | |
return source; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment