Browse Source

Take photo fix for api>=24

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

53
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
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);
Intent targetedIntent = new Intent(takePhotoIntent); List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
List<ResolveInfo> resInfo = this.getPackageManager().queryIntentActivities(takePhotoIntent, 0); for (ResolveInfo resolveInfo : resInfoList) {
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName; String packageName = resolveInfo.activityInfo.packageName;
targetedIntent.setPackage(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); bitmap = UploadsHelper.getImageResized(this, fileUri);
fileIcon = "jpg_image.gif"; int rotation = UploadsHelper.getRotation(this, fileUri);
bitmap = UploadsHelper.getImageResized(this, cacheFileUri);
int rotation = UploadsHelper.getRotation(this, cacheFileUri);
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());
fileUri = Uri.parse(UploadsHelper.createTempFile(this, cacheFileUri, newFilename));
newFilename += ".jpg";
filenameHolder.setText(newFilename);
filenameHolder.setVisibility(View.VISIBLE); filenameHolder.setVisibility(View.VISIBLE);
fileIcon = "jpg_image.gif";
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;

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