Browse Source

Uploads from camera init and fixes

pull/44/head
Apostolos Fanakis 6 years ago
parent
commit
61a8a7eb32
  1. 147
      app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java
  2. 179
      app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java

147
app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java

@ -4,7 +4,9 @@ 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.ResolveInfo;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
@ -17,7 +19,6 @@ import android.support.v7.app.AlertDialog;
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;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
@ -42,8 +43,12 @@ import java.io.File;
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.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.base.BaseActivity;
@ -206,8 +211,21 @@ public class UploadActivity extends BaseActivity {
builder.setTitle("Upload file"); builder.setTitle("Upload file");
builder.setItems(options, (dialog, item) -> { builder.setItems(options, (dialog, item) -> {
if (options[item].equals("Take photo")) { if (options[item].equals("Take photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); /*Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CODE_CAMERA); startActivityForResult(intent, REQUEST_CODE_CAMERA);*/
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePhotoIntent.putExtra("return-data", true);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(UploadsHelper.getTempFile(this)));
Intent targetedIntent = new Intent(takePhotoIntent);
List<ResolveInfo> resInfo = this.getPackageManager().queryIntentActivities(takePhotoIntent, 0);
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
targetedIntent.setPackage(packageName);
}
startActivityForResult(takePhotoIntent, REQUEST_CODE_CAMERA);
} else if (options[item].equals("Choose file")) { } else if (options[item].equals("Choose file")) {
String[] mimeTypes = {"image/jpeg", "text/html", "image/png", "image/jpg", "image/gif", String[] mimeTypes = {"image/jpeg", "text/html", "image/png", "image/jpg", "image/gif",
"application/pdf", "application/rar", "application/x-tar", "application/zip", "application/pdf", "application/rar", "application/x-tar", "application/zip",
@ -253,7 +271,7 @@ public class UploadActivity extends BaseActivity {
String tempFilePath = null; String tempFilePath = null;
if (uploadFilename != null) { if (uploadFilename != null) {
tempFilePath = createTempFile(uploadFilename); tempFilePath = UploadsHelper.createTempFile(this, fileUri, uploadFilename);
if (tempFilePath == null) { if (tempFilePath == null) {
//Something went wrong, abort //Something went wrong, abort
return; return;
@ -285,8 +303,8 @@ public class UploadActivity extends BaseActivity {
Exception exception) { Exception exception) {
Toast.makeText(context, "Upload failed", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Upload failed", Toast.LENGTH_SHORT).show();
if (finalTempFilePath != null) { if (finalTempFilePath != null) {
if (!deleteTempFile(finalTempFilePath)) { if (!UploadsHelper.deleteTempFile(finalTempFilePath)) {
Toast.makeText(context, "Failed to delete temp file", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Failed to delete temporary file", Toast.LENGTH_SHORT).show();
} }
} }
} }
@ -294,8 +312,8 @@ public class UploadActivity extends BaseActivity {
@Override @Override
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) { public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
if (finalTempFilePath != null) { if (finalTempFilePath != null) {
if (!deleteTempFile(finalTempFilePath)) { if (!UploadsHelper.deleteTempFile(finalTempFilePath)) {
Toast.makeText(context, "Failed to delete temp file", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Failed to delete temporary file", Toast.LENGTH_SHORT).show();
} }
} }
} }
@ -303,8 +321,8 @@ public class UploadActivity extends BaseActivity {
@Override @Override
public void onCancelled(Context context, UploadInfo uploadInfo) { public void onCancelled(Context context, UploadInfo uploadInfo) {
if (finalTempFilePath != null) { if (finalTempFilePath != null) {
if (!deleteTempFile(finalTempFilePath)) { if (!UploadsHelper.deleteTempFile(finalTempFilePath)) {
Toast.makeText(context, "Failed to delete temp file", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Failed to delete temporary file", Toast.LENGTH_SHORT).show();
} }
} }
} }
@ -381,7 +399,7 @@ public class UploadActivity extends BaseActivity {
fileUri = data.getData(); fileUri = data.getData();
if (fileUri != null) { if (fileUri != null) {
String filename = filenameFromUri(fileUri); String filename = UploadsHelper.filenameFromUri(this, fileUri);
selectFileButton.setText(filename); selectFileButton.setText(filename);
filename = filename.toLowerCase(); filename = filename.toLowerCase();
@ -404,11 +422,37 @@ public class UploadActivity extends BaseActivity {
fileIcon = "blank.gif"; fileIcon = "blank.gif";
} }
} }
} else if (requestCode == REQUEST_CODE_CAMERA && data != null) { } else if (requestCode == REQUEST_CODE_CAMERA) {
if (resultCode == Activity.RESULT_CANCELED) { if (resultCode == Activity.RESULT_CANCELED) {
return; return;
} }
//TODO
Bitmap bitmap;
File cacheImageFile = UploadsHelper.getTempFile(this);
if (resultCode == Activity.RESULT_OK) {
fileUri = Uri.fromFile(cacheImageFile);
fileIcon = "jpg_image.gif";
bitmap = UploadsHelper.getImageResized(this, fileUri);
int rotation = UploadsHelper.getRotation(this, fileUri);
bitmap = UploadsHelper.rotate(bitmap, rotation);
try {
FileOutputStream out = new FileOutputStream(cacheImageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
String newFilename = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.FRANCE).
format(new Date());
fileUri = Uri.parse(UploadsHelper.createTempFile(this, fileUri, newFilename));
newFilename += ".jpg";
selectFileButton.setText(newFilename);
}
} else if (requestCode == REQUEST_CODE_FIELDS_BUILDER) { } else if (requestCode == REQUEST_CODE_FIELDS_BUILDER) {
if (resultCode == Activity.RESULT_CANCELED) { if (resultCode == Activity.RESULT_CANCELED) {
return; return;
@ -422,83 +466,6 @@ public class UploadActivity extends BaseActivity {
} }
} }
@NonNull
private String filenameFromUri(Uri uri) {
String filename = null;
if (uri.getScheme().equals("content")) {
try (Cursor cursor = getContentResolver().query(uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
filename = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
}
}
if (filename == null) {
filename = uri.getPath();
int cut = filename.lastIndexOf('/');
if (cut != -1) {
filename = filename.substring(cut + 1);
}
}
return filename;
}
@SuppressWarnings("ResultOfMethodCallIgnored")
@Nullable
private String createTempFile(String newFilename) {
String oldFilename = filenameFromUri(fileUri);
String fileExtension = oldFilename.substring(oldFilename.indexOf("."));
String destinationFilename = android.os.Environment.getExternalStorageDirectory().getPath() +
File.separatorChar + "~tmp_mThmmy_uploads" + File.separatorChar + newFilename + fileExtension;
File tempDirectory = new File(android.os.Environment.getExternalStorageDirectory().getPath() +
File.separatorChar + "~tmp_mThmmy_uploads");
if (!tempDirectory.exists()) {
if (!tempDirectory.mkdirs()) {
Timber.w("Temporary directory build returned false in %s", UploadActivity.class.getSimpleName());
Toast.makeText(this, "Couldn't create temporary directory", Toast.LENGTH_SHORT).show();
return null;
}
}
InputStream inputStream;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
inputStream = getContentResolver().openInputStream(fileUri);
if (inputStream == null) {
Timber.w("Input stream was null, %s", UploadActivity.class.getSimpleName());
return null;
}
bufferedInputStream = new BufferedInputStream(inputStream);
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destinationFilename, false));
byte[] buf = new byte[1024];
bufferedInputStream.read(buf);
do {
bufferedOutputStream.write(buf);
} while (bufferedInputStream.read(buf) != -1);
} catch (IOException exception) {
exception.printStackTrace();
} finally {
try {
if (bufferedInputStream != null) bufferedInputStream.close();
if (bufferedOutputStream != null) bufferedOutputStream.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
return destinationFilename;
}
private boolean deleteTempFile(String destinationFilename) {
File file = new File(destinationFilename);
return file.delete();
}
private class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener { private class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
private ArrayList<UploadCategory> parentCategories, childCategories; private ArrayList<UploadCategory> parentCategories, childCategories;

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

@ -0,0 +1,179 @@
package gr.thmmy.mthmmy.activities.upload;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.OpenableColumns;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import timber.log.Timber;
class UploadsHelper {
private static final int DEFAULT_MIN_WIDTH_QUALITY = 400;
private static final String TEMP_IMAGE_NAME = "tempUploadFile.jpg";
@NonNull
static String filenameFromUri(Context context, Uri uri) {
String filename = null;
if (uri.getScheme().equals("content")) {
try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
filename = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
}
}
if (filename == null) {
filename = uri.getPath();
int cut = filename.lastIndexOf('/');
if (cut != -1) {
filename = filename.substring(cut + 1);
}
}
return filename;
}
@SuppressWarnings("ResultOfMethodCallIgnored")
@Nullable
static String createTempFile(Context context, Uri fileUri, String newFilename) {
String oldFilename = filenameFromUri(context, fileUri);
String fileExtension = oldFilename.substring(oldFilename.indexOf("."));
String destinationFilename = android.os.Environment.getExternalStorageDirectory().getPath() +
File.separatorChar + "~tmp_mThmmy_uploads" + File.separatorChar + newFilename + fileExtension;
File tempDirectory = new File(android.os.Environment.getExternalStorageDirectory().getPath() +
File.separatorChar + "~tmp_mThmmy_uploads");
if (!tempDirectory.exists()) {
if (!tempDirectory.mkdirs()) {
Timber.w("Temporary directory build returned false in %s", UploadActivity.class.getSimpleName());
Toast.makeText(context, "Couldn't create temporary directory", Toast.LENGTH_SHORT).show();
return null;
}
}
InputStream inputStream;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
inputStream = context.getContentResolver().openInputStream(fileUri);
if (inputStream == null) {
Timber.w("Input stream was null, %s", UploadActivity.class.getSimpleName());
return null;
}
bufferedInputStream = new BufferedInputStream(inputStream);
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destinationFilename, false));
byte[] buf = new byte[1024];
bufferedInputStream.read(buf);
do {
bufferedOutputStream.write(buf);
} while (bufferedInputStream.read(buf) != -1);
} catch (IOException exception) {
exception.printStackTrace();
} finally {
try {
if (bufferedInputStream != null) bufferedInputStream.close();
if (bufferedOutputStream != null) bufferedOutputStream.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
return destinationFilename;
}
static File getTempFile(Context context) {
File imageFile = new File(context.getExternalCacheDir(), TEMP_IMAGE_NAME);
//noinspection ResultOfMethodCallIgnored
imageFile.getParentFile().mkdirs();
return imageFile;
}
static boolean deleteTempFile(String destinationFilename) {
File file = new File(destinationFilename);
return file.delete();
}
/**
* Resize to avoid using too much memory loading big images (e.g.: 2560*1920)
**/
static Bitmap getImageResized(Context context, Uri selectedImage) {
Bitmap bm;
int[] sampleSizes = new int[]{5, 3, 2, 1};
int i = 0;
do {
bm = decodeBitmap(context, selectedImage, sampleSizes[i]);
i++;
} while (bm.getWidth() < DEFAULT_MIN_WIDTH_QUALITY && i < sampleSizes.length);
return bm;
}
private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = sampleSize;
AssetFileDescriptor fileDescriptor = null;
try {
fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
assert fileDescriptor != null;
return BitmapFactory.decodeFileDescriptor(
fileDescriptor.getFileDescriptor(), null, options);
}
static int getRotation(Context context, Uri imageUri) {
int rotation = 0;
try {
context.getContentResolver().notifyChange(imageUri, null);
ExifInterface exif = new ExifInterface(imageUri.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotation = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotation = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotation = 90;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return rotation;
}
static Bitmap rotate(Bitmap bm, int rotation) {
if (rotation != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
}
return bm;
}
}
Loading…
Cancel
Save