Last active
September 19, 2021 00:26
-
-
Save dirkvranckaert/70bc6812fe0388c8fe4f3bd5c56068c4 to your computer and use it in GitHub Desktop.
Taking a picture using a FileProvider
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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | |
<application | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name"> | |
... | |
<provider | |
android:name="android.support.v4.content.FileProvider" | |
android:authorities="${applicationId}.profileimage.fileprovider" | |
android:exported="false" | |
android:grantUriPermissions="true"> | |
<meta-data | |
android:name="android.support.FILE_PROVIDER_PATHS" | |
android:resource="@xml/file_paths"></meta-data> | |
</provider> | |
</application> | |
</manifest> |
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
<?xml version="1.0" encoding="utf-8"?> | |
<paths xmlns:android="http://schemas.android.com/apk/res/android"> | |
<external-files-path name="profile_images" path="images" /> | |
</paths> |
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 eu.vranckaert.profileimagedemo; | |
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.vranckaert.profileimagedemo.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()); | |
} | |
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); | |
// Samsung Galaxy S3 Fix | |
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); | |
for (ResolveInfo resolveInfo : resInfoList) { | |
String packageName = resolveInfo.activityInfo.packageName; | |
grantUriPermission(packageName, photoURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); | |
} | |
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