Browse Source

Take photo fix for api>=24

uploads
Apostolos Fanakis 6 years ago
parent
commit
30c161c789
  1. 67
      app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java
  2. 22
      app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java

67
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.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
@ -12,6 +13,7 @@ import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.FileProvider;
import android.support.v7.content.res.AppCompatResources; import android.support.v7.content.res.AppCompatResources;
import android.support.v7.preference.PreferenceManager; import android.support.v7.preference.PreferenceManager;
import android.support.v7.widget.AppCompatButton; import android.support.v7.widget.AppCompatButton;
@ -36,6 +38,7 @@ import org.jsoup.select.Elements;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -82,6 +85,7 @@ public class UploadActivity extends BaseActivity {
private String uploadFilename; private String uploadFilename;
private Uri fileUri; private Uri fileUri;
private String fileIcon; private String fileIcon;
private File photoFile = null;
//UI elements //UI elements
private MaterialProgressBar progressBar; private MaterialProgressBar progressBar;
@ -220,17 +224,29 @@ public class UploadActivity extends BaseActivity {
Drawable takePhotoDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_photo_camera_white_24dp); Drawable takePhotoDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_photo_camera_white_24dp);
takePhotoButton.setCompoundDrawablesRelativeWithIntrinsicBounds(takePhotoDrawable, null, null, null); takePhotoButton.setCompoundDrawablesRelativeWithIntrinsicBounds(takePhotoDrawable, null, null, null);
takePhotoButton.setOnClickListener(v -> { takePhotoButton.setOnClickListener(v -> {
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePhotoIntent.putExtra("return-data", true); // Ensure that there's a camera activity to handle the intent
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(UploadsHelper.getCacheFile(this))); if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
Intent targetedIntent = new Intent(takePhotoIntent); try {
List<ResolveInfo> resInfo = this.getPackageManager().queryIntentActivities(takePhotoIntent, 0); photoFile = UploadsHelper.createImageFile(this);
for (ResolveInfo resolveInfo : resInfo) { } catch (IOException ex) {
String packageName = resolveInfo.activityInfo.packageName; // Error occurred while creating the File
targetedIntent.setPackage(packageName); }
// 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<ResolveInfo> 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); FloatingActionButton uploadFAB = findViewById(R.id.upload_fab);
@ -400,17 +416,14 @@ public class UploadActivity extends BaseActivity {
} }
Bitmap bitmap; Bitmap bitmap;
File cacheImageFile = UploadsHelper.getCacheFile(this); fileUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", photoFile);
Uri cacheFileUri = Uri.fromFile(cacheImageFile);
fileIcon = "jpg_image.gif";
bitmap = UploadsHelper.getImageResized(this, cacheFileUri); bitmap = UploadsHelper.getImageResized(this, fileUri);
int rotation = UploadsHelper.getRotation(this, cacheFileUri); int rotation = UploadsHelper.getRotation(this, fileUri);
bitmap = UploadsHelper.rotate(bitmap, rotation); bitmap = UploadsHelper.rotate(bitmap, rotation);
try { try {
FileOutputStream out = new FileOutputStream(cacheImageFile); FileOutputStream out = new FileOutputStream(photoFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush(); out.flush();
out.close(); out.close();
@ -418,15 +431,9 @@ public class UploadActivity extends BaseActivity {
e.printStackTrace(); e.printStackTrace();
} }
String newFilename = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.FRANCE). filenameHolder.setText(photoFile.getName());
format(new Date()); filenameHolder.setVisibility(View.VISIBLE);
fileUri = Uri.parse(UploadsHelper.createTempFile(this, cacheFileUri, newFilename)); fileIcon = "jpg_image.gif";
newFilename += ".jpg";
filenameHolder.setText(newFilename);
filenameHolder.setVisibility(View.VISIBLE);
UploadsHelper.deleteCacheFiles(this);
} else if (requestCode == AFR_REQUEST_CODE_FIELDS_BUILDER) { } else if (requestCode == AFR_REQUEST_CODE_FIELDS_BUILDER) {
if (resultCode == Activity.RESULT_CANCELED) { if (resultCode == Activity.RESULT_CANCELED) {
return; return;
@ -579,10 +586,10 @@ public class UploadActivity extends BaseActivity {
tmpSpinnerArray[i] = uploadRootCategories.get(i).getCategoryTitle(); tmpSpinnerArray[i] = uploadRootCategories.get(i).getCategoryTitle();
} }
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(BaseApplication.getInstance().getApplicationContext(), ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(BaseApplication.getInstance().getApplicationContext(),
R.layout.spinner_item, tmpSpinnerArray); R.layout.spinner_item, tmpSpinnerArray);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item); spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
rootCategorySpinner.setAdapter(spinnerArrayAdapter); rootCategorySpinner.setAdapter(spinnerArrayAdapter);
//Sets bundle selection //Sets bundle selection
if (bundleCategory != null) { if (bundleCategory != null) {

22
app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java

@ -1,6 +1,7 @@
package gr.thmmy.mthmmy.activities.upload; package gr.thmmy.mthmmy.activities.upload;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor; import android.content.res.AssetFileDescriptor;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@ -12,6 +13,7 @@ import android.os.Environment;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.widget.Toast; import android.widget.Toast;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
@ -21,13 +23,33 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import timber.log.Timber; import timber.log.Timber;
import static android.support.v4.content.FileProvider.getUriForFile;
class UploadsHelper { class UploadsHelper {
private static final int DEFAULT_MIN_WIDTH_QUALITY = 400; private static final int DEFAULT_MIN_WIDTH_QUALITY = 400;
private static final String CACHE_IMAGE_NAME = "tempUploadFile.jpg"; 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 @NonNull
static String filenameFromUri(Context context, Uri uri) { static String filenameFromUri(Context context, Uri uri) {
String filename = null; String filename = null;

Loading…
Cancel
Save