Skip to content

Instantly share code, notes, and snippets.

@makovkastar
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makovkastar/1cd106c0af864f03f079 to your computer and use it in GitHub Desktop.
Save makovkastar/1cd106c0af864f03f079 to your computer and use it in GitHub Desktop.
FilePicker
package com.melnykov.booktracker.util;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;
import com.melnykov.booktracker.BookTrackerApplication;
import com.melnykov.booktracker.R;
import timber.log.Timber;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FilePicker {
private static final String JPEG_FILE_PREFIX = "IMG_";
private static final String JPEG_FILE_SUFFIX = ".jpg";
private static final int REQUEST_CODE_PICK_IMAGE = 100;
private static final int REQUEST_CODE_TAKE_IMAGE = 200;
private static final int REQUEST_CODE_PICK_FILE = 300;
private static String sLastCameraPhotoPath;
private FilePicker() {
}
public static void pickCsvFile(Activity activity) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/csv");
activity.startActivityForResult(intent, REQUEST_CODE_PICK_FILE);
}
public static void pickImage(Activity activity) {
Intent intentPick = createPickImageIntent();
// Ensure that there's an activity to handle the intent
if (intentPick.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivityForResult(intentPick, REQUEST_CODE_PICK_IMAGE);
} else {
Toast.makeText(activity, R.string.error_no_gallery, Toast.LENGTH_SHORT).show();
}
}
public static void takeImage(Activity activity) {
Intent intentTake = createTakeImageIntent();
// Ensure that there's a camera activity to handle the intent
if (intentTake.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivityForResult(intentTake, REQUEST_CODE_TAKE_IMAGE);
} else {
Toast.makeText(activity, R.string.error_no_camera, Toast.LENGTH_SHORT).show();
}
}
public static String parseImagePath(int requestCode, int resultCode, Intent intent) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CODE_PICK_IMAGE) {
return parseGalleryImagePath(intent);
} else if (requestCode == REQUEST_CODE_TAKE_IMAGE) {
return sLastCameraPhotoPath;
}
}
return null;
}
public static String parseFilePath(int requestCode, int resultCode, Intent intent) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CODE_PICK_FILE) {
return parseFilePath(intent);
}
}
return null;
}
private static String parseGalleryImagePath(Intent intent) {
if (intent != null && intent.getData() != null) {
return intent.getData().toString();
}
return null;
}
private static String parseFilePath(Intent intent) {
if (intent != null && intent.getData() != null) {
return intent.getData().getPath();
}
return null;
}
@SuppressLint("NewApi")
private static Intent createPickImageIntent() {
boolean kitkat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
Intent intent = new Intent(kitkat? Intent.ACTION_OPEN_DOCUMENT: Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
return intent;
}
private static Intent createTakeImageIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// TODO: add error toast
Timber.e(ex, "Cannot create an image file.");
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
}
return takePictureIntent;
}
private static File createImageFile() throws IOException {
File storageDir;
if (Device.isExternalStorageWritable()) {
storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
} else {
storageDir = BookTrackerApplication.getInstance().getFilesDir();
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
File image = File.createTempFile(
imageFileName, /* prefix */
JPEG_FILE_SUFFIX, /* suffix */
storageDir /* directory */
);
sLastCameraPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
}
FilePicker.pickImage(getActivity());
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
bookCoverImage = FilePicker.parseImagePath(requestCode, resultCode, data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment