From 30c161c7892a8a4000f215d07030d72e1870a57d Mon Sep 17 00:00:00 2001 From: Apostolof Date: Mon, 3 Sep 2018 14:10:41 +0300 Subject: [PATCH] Take photo fix for api>=24 --- .../activities/upload/UploadActivity.java | 67 ++++++++++--------- .../activities/upload/UploadsHelper.java | 22 ++++++ 2 files changed, 59 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java index ce93b6b1..96111a0e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; @@ -12,6 +13,7 @@ import android.os.AsyncTask; import android.os.Bundle; import android.provider.MediaStore; import android.support.design.widget.FloatingActionButton; +import android.support.v4.content.FileProvider; import android.support.v7.content.res.AppCompatResources; import android.support.v7.preference.PreferenceManager; import android.support.v7.widget.AppCompatButton; @@ -36,6 +38,7 @@ import org.jsoup.select.Elements; import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; @@ -82,6 +85,7 @@ public class UploadActivity extends BaseActivity { private String uploadFilename; private Uri fileUri; private String fileIcon; + private File photoFile = null; //UI elements private MaterialProgressBar progressBar; @@ -220,17 +224,29 @@ public class UploadActivity extends BaseActivity { Drawable takePhotoDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_photo_camera_white_24dp); takePhotoButton.setCompoundDrawablesRelativeWithIntrinsicBounds(takePhotoDrawable, null, null, null); takePhotoButton.setOnClickListener(v -> { - Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); - takePhotoIntent.putExtra("return-data", true); - takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(UploadsHelper.getCacheFile(this))); - - Intent targetedIntent = new Intent(takePhotoIntent); - List resInfo = this.getPackageManager().queryIntentActivities(takePhotoIntent, 0); - for (ResolveInfo resolveInfo : resInfo) { - String packageName = resolveInfo.activityInfo.packageName; - targetedIntent.setPackage(packageName); + Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + // Ensure that there's a camera activity to handle the intent + if (takePictureIntent.resolveActivity(getPackageManager()) != null) { + // Create the File where the photo should go + try { + photoFile = UploadsHelper.createImageFile(this); + } catch (IOException ex) { + // Error occurred while creating the File + } + // Continue only if the File was successfully created + if (photoFile != null) { + Uri photoURI = FileProvider.getUriForFile(this, getPackageName() + ".provider", photoFile); + takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); + + List resInfoList = getPackageManager().queryIntentActivities(takePictureIntent, 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); + } + + startActivityForResult(takePictureIntent, AFR_REQUEST_CODE_CAMERA); + } } - startActivityForResult(takePhotoIntent, AFR_REQUEST_CODE_CAMERA); }); FloatingActionButton uploadFAB = findViewById(R.id.upload_fab); @@ -400,17 +416,14 @@ public class UploadActivity extends BaseActivity { } Bitmap bitmap; - File cacheImageFile = UploadsHelper.getCacheFile(this); - - Uri cacheFileUri = Uri.fromFile(cacheImageFile); - fileIcon = "jpg_image.gif"; + fileUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", photoFile); - bitmap = UploadsHelper.getImageResized(this, cacheFileUri); - int rotation = UploadsHelper.getRotation(this, cacheFileUri); + bitmap = UploadsHelper.getImageResized(this, fileUri); + int rotation = UploadsHelper.getRotation(this, fileUri); bitmap = UploadsHelper.rotate(bitmap, rotation); try { - FileOutputStream out = new FileOutputStream(cacheImageFile); + FileOutputStream out = new FileOutputStream(photoFile); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); @@ -418,15 +431,9 @@ public class UploadActivity extends BaseActivity { e.printStackTrace(); } - String newFilename = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.FRANCE). - format(new Date()); - fileUri = Uri.parse(UploadsHelper.createTempFile(this, cacheFileUri, newFilename)); - - newFilename += ".jpg"; - filenameHolder.setText(newFilename); - filenameHolder.setVisibility(View.VISIBLE); - - UploadsHelper.deleteCacheFiles(this); + filenameHolder.setText(photoFile.getName()); + filenameHolder.setVisibility(View.VISIBLE); + fileIcon = "jpg_image.gif"; } else if (requestCode == AFR_REQUEST_CODE_FIELDS_BUILDER) { if (resultCode == Activity.RESULT_CANCELED) { return; @@ -579,10 +586,10 @@ public class UploadActivity extends BaseActivity { tmpSpinnerArray[i] = uploadRootCategories.get(i).getCategoryTitle(); } - ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(BaseApplication.getInstance().getApplicationContext(), - R.layout.spinner_item, tmpSpinnerArray); - spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item); - rootCategorySpinner.setAdapter(spinnerArrayAdapter); + ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(BaseApplication.getInstance().getApplicationContext(), + R.layout.spinner_item, tmpSpinnerArray); + spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item); + rootCategorySpinner.setAdapter(spinnerArrayAdapter); //Sets bundle selection if (bundleCategory != null) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java index 9529d402..ebb9f449 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java @@ -1,6 +1,7 @@ package gr.thmmy.mthmmy.activities.upload; import android.content.Context; +import android.content.Intent; import android.content.res.AssetFileDescriptor; import android.database.Cursor; import android.graphics.Bitmap; @@ -12,6 +13,7 @@ import android.os.Environment; import android.provider.OpenableColumns; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.content.FileProvider; import android.widget.Toast; import java.io.BufferedInputStream; @@ -21,13 +23,33 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; import timber.log.Timber; +import static android.support.v4.content.FileProvider.getUriForFile; + class UploadsHelper { private static final int DEFAULT_MIN_WIDTH_QUALITY = 400; private static final String CACHE_IMAGE_NAME = "tempUploadFile.jpg"; + static File createImageFile(Context context) throws IOException { + // Create an image file name + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.FRANCE).format(new Date()); + String imageFileName = "JPEG_" + timeStamp + ".jpg"; + + + return new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), imageFileName); + } + + void galleryAddPic(Context context, Uri photo) { + Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); + mediaScanIntent.setData(photo); + context.sendBroadcast(mediaScanIntent); + } + @NonNull static String filenameFromUri(Context context, Uri uri) { String filename = null;