Cropper returns null Uri - android

My app has possibility to change user avatar (with latter uploading it to web service). But on some devices Cropper ("com.android.camera.action.CROP" Intent) starts, but returns null Uri. Could u point me what i've done wrong?
1st user picks photo with:
public static void pickPhoto(final Context context) {
Intent pickImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
pickImageIntent.setType("image/*");
((ActionBarActivity) context).startActivityForResult(Intent.createChooser(pickImageIntent, "Select Avatar"), SettingsActivity.REQUEST_CODE_PICK_AVATAR);
}
Then after activity get's RESULT_OK (selected image uri) - it starts cropper:
public static void startCropper(final Context context, final Uri imageToCrop) {
final Intent intent = new Intent("com.android.camera.action.CROP");
intent.setData(imageToCrop);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context)));
intent.putExtra("return-data", true);
((ActionBarActivity) context).startActivityForResult(intent, SettingsActivity.REQUEST_CODE_CROP_AVATAR);
}
which returns RESULT_OK with null getData():
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_CROP_AVATAR && resultCode == RESULT_OK) {
Uri uri = data.getData(); // uri is null on some devices
}
}
My getTempFile method:
public static File getTempFile(Context context) {
File cacheDir;
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
cacheDir = context.getCacheDir();
} else {
cacheDir = context.getExternalCacheDir();
}
File file = new File(cacheDir, "temp_avatar.jpg");
try {
file.createNewFile();
} catch (IOException e) {
}
return file;
}

As mentioned in question comments by greenapps, it's just need to place additional check in onActivityResult() if uri is null and then getExtra("data"), like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_CROP_AVATAR && resultCode == RESULT_OK) {
Uri uri = data.getData();
if (null != uri) {
// do something with uri
} else {
Bitmap bitmap = data.getExtra("data");
// get uri from bitmap and do something with uri
}
}
}
Code tested in my app and works fine.

Related

Android - Check button after taking picture using camera doesn't do anything

So.. I'm trying to choose a picture using both camera and gallery, then pass it to an external library to crop image.
The problem lies with saving image after taking it with the camera. I've managed to get the camera running and take an image with it, then it display the image and the default Android Ok and back button. The Ok button responds to touch (as touch effect can be seen) but it doesn't do anything.
Here's the code for getting the file ready to save
date = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ddMMyyyyHHmmss");
dateString = simpleDateFormat.format(date);
dateBuilder = new StringBuilder().append(dateString).append(".jpg");
SAMPLE_CROPPED_IMAGE_NAME = dateBuilder.toString();
final String cameraDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/SamplePics/";
newDir = new File(cameraDir);
newDir.mkdirs();
This is the code for camera function
RelativeLayout.OnClickListener photoCameraWrapperHandler = new RelativeLayout.OnClickListener(){
#Override
public void onClick(View v) {
//Intent intent = new Intent(SignupStepThreeActivity.this, SignupStepFourActivity.class);
//startActivity(intent);
String cameraFile = SAMPLE_CROPPED_IMAGE_NAME;
newFile = new File(cameraFile);
try {
newFile.createNewFile();
}
catch (IOException e)
{
}
Uri outputFileUri = Uri.fromFile(newFile);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
};
And here's the onActivityResult :
private static final int CAMERA_REQUEST = 1888;
private static final int REQUEST_SELECT_PICTURE = 0x01;
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_SELECT_PICTURE || requestCode == CAMERA_REQUEST) {
final Uri selectedUri = data.getData();
if (selectedUri != null) {
startCropActivity(data.getData());
} else {
Toast.makeText(SignupStepThreeActivity.this, R.string.toast_cannot_retrieve_selected_image, Toast.LENGTH_SHORT).show();
}
} else if (requestCode == UCrop.REQUEST_CROP) {
handleCropResult(data);
}
}
if (resultCode == UCrop.RESULT_ERROR) {
handleCropError(data);
}
}
Several updates before, it crashes when I click on camera button, I suspected it was because of I kind of take the uri of the image from storage but I haven't created the folder. Now I finally managed to get the camera running but not saving. The create folder part works tho..
public String getCamerPath(Context context) {
SharedPreferences prefs = context.getSharedPreferences("setCamerPath", 0);
String value = prefs.getString("getCamerPath", "");
return value;
}
public void setCamerPath(Context context, String value)
{
SharedPreferences prefs = context.getSharedPreferences("setCamerPath", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("getCamerPath", value);
editor.commit();
}
Uri outputFileUri = Uri.fromFile(newFile);
setCamerPath(this, outputFileUri.getPath());
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_SELECT_PICTURE || requestCode == CAMERA_REQUEST) {
startCropActivity(getCamerPath(this));
} else if (requestCode == UCrop.REQUEST_CROP) {
handleCropResult(data);
}
}
if (resultCode == UCrop.RESULT_ERROR) {
handleCropError(data);
}
}
In the camera OnClickListener, I commented some stuffs like this :
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//Uri outputFileUri = Uri.fromFile(newFile);
//cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
//setCamerPath(SignupStepThreeActivity.this, outputFileUri.getPath());
startActivityForResult(cameraIntent, CAMERA_REQUEST);
In my onActivityResult, I've modified a part of CAMERA_REQUEST :
else if (requestCode == CAMERA_REQUEST) {
Uri capturedImageUri = data.getData();
Bitmap bitmap;
if (capturedImageUri != null) {
startCropActivity(capturedImageUri);
} else {
Toast.makeText(SignupStepThreeActivity.this, R.string.toast_cannot_retrieve_selected_image, Toast.LENGTH_SHORT).show();
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
Uri imageUri = getImageUri(SignupStepThreeActivity.this, bitmap);
startCropActivity(imageUri);
}
}
So basically as I've been reading around for the past 4 hours, data is Intent. It will always be not null BUT will not always contain Uri itself for the image. Because I need the Uri for a library I'm using, I use a method called getImageUri from another Stackoverflow answer.
public static Uri getImageUri(Context inContext, Bitmap inImage)
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
Now the problem lies with bitmap image quality after taking the image, I'm going to tinker around with it and be back with an update.
This can also resolve the problem
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d("camera", "onSaveInstance");
// save file url in bundle as it will be null on screen orientation
// changes
outState.putParcelable("file_uri", fileUri);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d("camera", "onRestoreInstance");
// get the file url
fileUri = savedInstanceState.getParcelable("file_uri");
}

Out of Memory error while loading images. [Android]

I want to pick image from the gallery and use it in my App as Profile Picture. So far I've tried this.
Here's my InfoGet class. The loadImagefromGallery gets called on click.
public void loadImagefromGallery(View view) {
/* Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD);*/
Intent gallery_Intent = new Intent(getApplicationContext(), GalleryUtil.class);
startActivityForResult(gallery_Intent, GALLERY_ACTIVITY_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_ACTIVITY_CODE) {
if(resultCode == Activity.RESULT_OK){
String picturePath = data.getStringExtra("picturePath");
//perform Crop on the Image Selected from Gallery
performCrop(picturePath);
}
}
if (requestCode == RESULT_CROP ) {
if(resultCode == Activity.RESULT_OK){
Bundle extras = data.getExtras();
Bitmap selectedBitmap = extras.getParcelable("data");
// Set The Bitmap Data To ImageView
pro.setImageBitmap(selectedBitmap);
pro.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
}
private void performCrop(String picUri) {
try {
//Start Crop Activity
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
File f = new File(picUri);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 280);
cropIntent.putExtra("outputY", 280);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, RESULT_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
And here's my GalleryUtil code
public class GalleryUtil extends Activity {
private final static int RESULT_SELECT_IMAGE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
private static final String TAG = "GalleryUtil";
String mCurrentPhotoPath;
File photoFile = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
//Pick Image From Gallery
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_SELECT_IMAGE);
}catch(Exception e){
e.printStackTrace();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case RESULT_SELECT_IMAGE:
if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
try{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
//return Image Path to the Main Activity
Intent returnFromGalleryIntent = new Intent();
returnFromGalleryIntent.putExtra("picturePath",picturePath);
setResult(RESULT_OK,returnFromGalleryIntent);
finish();
}catch(Exception e){
e.printStackTrace();
Intent returnFromGalleryIntent = new Intent();
setResult(RESULT_CANCELED, returnFromGalleryIntent);
finish();
}
}else{
Log.i(TAG, "RESULT_CANCELED");
Intent returnFromGalleryIntent = new Intent();
setResult(RESULT_CANCELED, returnFromGalleryIntent);
finish();
}
break;
}
}
}
I need help can anyone just make some changes in the existing code. I do not want to rebuild anything from scratch. I want to optimise this image picker before I release the next update. The Existing code work okay for devices with decent amount of RAM like 1GB or so.
Please help me.
And also explain what you did and how it works. Thanks a LOT.
I did that method read images from path and solve this issue, maybe it help you:
public static Bitmap readBitmap(Uri selectedImage, int resizeFactor)
{
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = resizeFactor;
AssetFileDescriptor fileDescriptor = null;
try
{
fileDescriptor = context.getContentResolver().openAssetFileDescriptor(selectedImage, "r");
} catch (FileNotFoundException e)
{
e.printStackTrace();
} finally
{
try
{
bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
fileDescriptor.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
return bm;
}
The atributes are Uri selectedImage and int resizeFactor;
on selectedImage you need to put a Path from image and on resizeFactor put a int number, Normally I use number 10, try with that or try with another one (down/up)
Now if you want to put on ImageView you can use that.
ImageView.setImageBitmap(readBitmap(Path, 10));
Later you need clear Bitmap content to avoid OutOfMemory, use that method:
public static void clearBitmap(Bitmap bm)
{
bm.recycle();
}

Output Uri from this Android crop image library?

I'm trying to implement SoundCloud's image crop to my app. Here's what I'm doing so far:
call Crop.pickImage((Activity) this) to pick an image
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
Uri inputUri = data.getData();
new Crop(inputUri).output(outputUri).asSquare().start((Activity) context);
}
else if (requestCode == Crop.REQUEST_CROP && resultCode == RESULT_OK) {
Uri cropped = data.getData();
}
}
At this point I don't know what should be the outputUri. I can just use the same inputUri as the output, but that would overwrite the original image file. I want to create a new file instead. But I can't create new Uri since Android Studio tells me the newly initialized Uri is abstract and can't be used.
I used following methods for my cropping image library for URI creation as well. Hopefully it will helps you:
private void fileSaving(){
String stateEnvironment = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(stateEnvironment)) {
mFileTemp = ChatUtils.getOutputMediaFile(101);//new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE_NAME);
} else {
mFileTemp = new File(getFilesDir(), sTEMP_PHOTO_FILE_NAME);
}
}
private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
Uri mImageCaptureUri = null;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mImageCaptureUri = Uri.fromFile(mFileTemp);
} else {
// mImageCaptureUri =
// InternalStorageContentProvider.CONTENT_URI;
}
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, mREQUEST_CODE_TAKE_PICTURE);
} catch (ActivityNotFoundException e) {
}
}

Crop Images issues with Capture Image using Android

I am using Camera Function to Capture Images 2 times from different buttons. After Captured Image, It goes to Crop Option where user can crop the Image. It is also working fine.
But, now issue is that when user capture second image then App redirects to Crop image and show first image only rather than second capture image. I am also deleting image after It has been set to ImageView. Don't know what is the wrong ?
Please Help me to solve this issue.
My Code :
Bitmap bm_PhotoProof = null;
Bitmap bm_AddressProof = null;
private static final String TEMP_PHOTO_FILE = "tmp_ihis.jpg";
private static final int REQ_CODE_PICK_IMAGE_PHOTOPROOF = 0;
private static final int REQ_CODE_PICK_IMAGE_ADDRESSPROOF = 1;
imgPhotoProof.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
cameraIntent.putExtra("outputFormat",
Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(cameraIntent,
REQ_CODE_PICK_IMAGE_PHOTOPROOF);
}
});
imgAddressProof.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
cameraIntent.putExtra("outputFormat",
Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(cameraIntent,
REQ_CODE_PICK_IMAGE_ADDRESSPROOF);
}
});
private Uri getTempUri() {
return Uri.fromFile(getTempFile());
}
private File getTempFile() {
File f = new File(Environment.getExternalStorageDirectory(),
TEMP_PHOTO_FILE);
try {
f.createNewFile();
} catch (IOException e) {
Log.e("getTempFile()->", e.getMessage().toString());
}
return f;
}
public void cropCapturedImage(Uri picUri, String Type) {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 0);
cropIntent.putExtra("aspectY", 0);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
if (Type.equals("Photo")) {
startActivityForResult(cropIntent, 5);
} else {
startActivityForResult(cropIntent, 6);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQ_CODE_PICK_IMAGE_PHOTOPROOF) {
File tempFile = getTempFile();
cropCapturedImage(Uri.fromFile(tempFile), "Photo");
} else if (requestCode == REQ_CODE_PICK_IMAGE_ADDRESSPROOF) {
File tempFile = getTempFile();
cropCapturedImage(Uri.fromFile(tempFile), "Address");
}
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == 5) {
if (data != null) {
Bundle extras = data.getExtras();
bm_PhotoProof = extras.getParcelable("data");
}
imgPhotoProof_Pic.setImageBitmap(bm_PhotoProof);
File tempFile = getTempFile();
if (tempFile.exists()) {
tempFile.delete();
}
}
if (requestCode == 6) {
if (data != null) {
Bundle extras = data.getExtras();
bm_AddressProof = extras.getParcelable("data");
}
imgAddressProof_Pic.setImageBitmap(bm_AddressProof);
File tempFile = getTempFile();
if (tempFile.exists()) {
tempFile.delete();
}
}
}
}
}
I have solved this issue.Before, i was deleting just file. Now, I have also cleared cached and it solved my problem.
File tempFile = getTempFile();
if (tempFile.exists()) {
tempFile.delete();
}
to
File tempFile = getTempFile();
File cacheDir = getActivity().getCacheDir();
File file = new File(cacheDir, getTempFile().toString());
file.delete();

Android camera capture activity returns null Uri

This code worked on samsung before but now that i'm using Nexus One with Android 2.3.6, it's crashing as soon as I take a picture and click ok or choose a photo from gallery. Stacktrace shows a null pointer exception on the Uri.
My code for the activating the camera is as follows:
public void activateCamera(View view){
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// start the image capture Intent
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == CHOOSE_IMAGE_ACTIVITY_REQUEST_CODE || requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
&& resultCode == RESULT_OK && null != data) {
selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bits = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize;
try {
bits = BitmapFactory.decodeStream(new FileInputStream(picturePath),null,options);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Any idea what could be the problem?
Thanks!
You have to tell the camera, where to save the picture and remeber the uri yourself:
private Uri mMakePhotoUri;
private File createImageFile() {
// return a File object for your image.
}
private void makePhoto() {
try {
File f = createImageFile();
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMakePhotoUri = Uri.fromFile(f);
i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
startActivityForResult(i, REQUEST_MAKE_PHOTO);
} catch (IOException e) {
Log.e(TAG, "IO error", e);
Toast.makeText(getActivity(), R.string.error_writing_image, Toast.LENGTH_LONG).show();
}
}
#Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_MAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
// do something with mMakePhotoUri
}
return;
default: // do nothing
super.onActivityResult(requestCode, resultCode, data);
}
}
You should save the value of mMakePhotoUri over instance states withing onCreate() and onSaveInstanceState().
Inject this extra into the Intent that called onActivityResult and the system will do all the heavy lifting for you.
File f = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
Doing this makes it as easy as this to retrieve the photo as a bitmap.
private void handleSmallCameraPhoto(Intent intent) {
Bundle extras = intent.getExtras();
mImageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(mImageBitmap);
}
dont pass any extras, just define the path where you have placed or saved the file directly in onActivityResult
public void openCamera() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = createImageFile();
boolean isDirectoryCreated = file.getParentFile().mkdirs();
Log.d("", "openCamera: isDirectoryCreated: " + isDirectoryCreated);
if (Build.VERSION.SDK_INT >= 23) {
tempFileUri = FileProvider.getUriForFile(getActivity().getApplicationContext(),
"com.scanlibrary.provider", // As defined in Manifest
file);
} else {
tempFileUri = Uri.fromFile(file);
}
try
{
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, ScanConstants.START_CAMERA_REQUEST_CODE);
}
catch (Exception e)
{
}
}
private File createImageFile() {
clearTempImages();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new
Date());
File file = new File(ScanConstants.IMAGE_PATH, "IMG_" + timeStamp +
".jpg");
fileUri = Uri.fromFile(file);
return file;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = null ;
if (resultCode == Activity.RESULT_OK ) {
try {
if (Build.VERSION.SDK_INT >= 23) {
tempFileUri = FileProvider.getUriForFile(getActivity().getApplicationContext(),
"com.scanlibrary.provider", // As defined in Manifest
file);
} else {
tempFileUri = Uri.fromFile(file);
}
bitmap = getBitmap(tempFileUri);
bitmap = getBitmap(data.getData());
} catch (Exception e) {
e.printStackTrace();
}
} else {
getActivity().finish();
}
if (bitmap != null) {
postImagePick(bitmap);
}
}

Categories

Resources