Weird issues with Samsung S3 native camera for result crashing - android

My app has the following code to invoke a capture from the native camera; startActivityForResult
It has been tested on a nexus 5, HTC 1, nexus 7, Samsung S4, and Samsung S3. It works great on every device except, the S3. On the S3 the app crashed on return to the starting activity
the crash:
03-07 13:09:21.297: E/ActivityThread(6535): Activity com.DRPMapViewActivity
has leaked ServiceConnection android.media.MediaScannerConnection#42bd73d8 that
was originally bound here
03-07 13:09:21.297: E/ActivityThread(6535): android.app.ServiceConnectionLeaked:
Activity com.DRPMapViewActivity has leaked ServiceConnection
android.media.MediaScannerConnection#42bd73d8 that was originally
bound here
my code
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = new File(g.kPhotoDirectory);
// File storageDir = new
// File(Environment.getExternalStoragePublicDirectory(
// Environment.DIRECTORY_DCIM), "Drop");
storageDir.mkdirs();
File image = File.createTempFile(imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
photoFileFromCapture = null;
try {
photoFileFromCapture = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFileFromCapture != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFileFromCapture));
startActivityForResult(takePictureIntent,
g.kRequestImageCaptureCode);
}
}
}
private void dispatchChoosePictureIntent() {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*");
// Intent chooserIntent = Intent.createChooser(i,"Image Chooser");
startActivityForResult(i, g.kRequestImageChooserCode);
}
my onActivityResult looks like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == g.kGenericRequestCode) {
if (resultCode == g.kKillMeResultCode) {
finish();
}
Log.v("activityResult", "requestcode:" + requestCode
+ " resultCode:" + resultCode + " data:" + data);
super.onActivityResult(requestCode, resultCode, data);
}
if (requestCode == g.kRequestImageChooserCode
&& resultCode == Activity.RESULT_OK) {
Uri imageUri = data.getData();
Log.v("CAPTURE", "uri:" + imageUri);
String filePath = getRealPathFromURI(imageUri);
Intent i = new Intent(DRPMapViewActivity.this,
DRPCreateDropActivity.class);
i.putExtra("USER", _user);
i.putExtra("LATLNG", getLocationForCreateDrop());
i.putExtra("FILE_PATH", filePath);
i.putExtra("TYPE", CreateDropType.kImageFile);
startActivity(i);
overridePendingTransition(R.anim.slide_in_right,
R.anim.slide_out_left);
}
if (requestCode == g.kRequestImageCaptureCode) {
Log.v("CAPTURE RESULT", "result:" + resultCode);
if(resultCode== Activity.RESULT_OK){
MediaScannerConnection.scanFile(this,
new String[] { mCurrentPhotoPath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
Intent i = new Intent(DRPMapViewActivity.this,
DRPCreateDropActivity.class);
i.putExtra("USER", _user);
i.putExtra("LATLNG", getLocationForCreateDrop());
i.putExtra("FILE_PATH", mCurrentPhotoPath);
i.putExtra("TYPE", CreateDropType.kImageFile);
startActivity(i);
overridePendingTransition(R.anim.slide_in_right,
R.anim.slide_out_left);
if (data != null) {
Log.v("CAPTURE RESULT", "data:" + data.getData());
}
}
}
}

So the problem seems to be that for what ever reason the Samsung S3's native camera is not returning Data when setting result. So while you get the appropriate result code there is no actual data being passed back. To fix this in my result listener, i check to see if the data is null. The data is suppose to be the file path of the photo taken. If the path is null && the result code is RESULT_OK, I just reiterate through the directory that I told the camera to save the picture in and get the path of the last file created.:
if(heroFilePath==null){
File dir = new File(g.kPhotoDirectory);
File[] files = dir.listFiles();
File lastModifiedFile = files[0];
for (int i = 1; i < files.length; i++) {
if (lastModifiedFile.lastModified() < files[i].lastModified()) {
lastModifiedFile = files[i];
}
}
heroFilePath = lastModifiedFile.getAbsolutePath();
}

Related

Image size and dimensions reduce when I capture from camera

Using this to open camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
and in OnActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK)
{
onCaptureImageResult(data);
}
}
private void onCaptureImageResult(Intent data) {
if (data != null) {
cameraBitMap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
cameraBitMap.compress(Bitmap.CompressFormat.JPEG,100, bytes);
cameraFilePath = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
cameraFilePath.createNewFile();
fo = new FileOutputStream(cameraFilePath);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(cameraFilePath.getAbsolutePath(), options);
imageHeight = options.outHeight;
imageWidth = options.outWidth;
camImagePath = String.valueOf(cameraFilePath);
String fileName = camImagePath.substring(camImagePath.lastIndexOf("/") + 1);
txt_FileName.setText(fileName);
txt_FileName.setVisibility(View.VISIBLE);
btn_ImageTest.setImageBitmap(cameraBitMap);
}
}
I want the original size image to display when I capture it from the camera.
Use Intent like this. Here first give image name .
//declare globally
private static final int CAMERA_CODE = 101 ;
private Uri mImageCaptureUri;
Start Intent as below.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp1.jpg");
if (f.exists()) {
f.delete();
}
mImageCaptureUri = Uri.fromFile(f);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(intent, CAMERA_CODE);
And in onActivityResult :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CODE && resultCode == RESULT_OK) {
System.out.println("Camera Image URI : " + mImageCaptureUri);
String path = mImageCaptureUri.getPath();
if (new File(path).exists()) {
Toast.makeText(MainActivity.this, "Path is Exists..", Toast.LENGTH_LONG).show();
}
btn_ImageTest.setImageURI(mImageCaptureUri);
}
}
You need to create temporary file before you start Camera Activity Intent.
Anything that using below code or similar like that after you take image using camera actually is a thumbnail image which is reduced the original quality.
This is the code that I meant:
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
Refer Here for more info
This are the solution that you want
First You need to create temporary file.
Convert the temporary file into URI
Pass URI data to Camera intent by using putExtra method.
Open Camera Activity intent.
Handle onActivityResult if user cancel or accept the image from Camera Activity intent.
Below are the method you can use for step by step mentioned above.
Create Temporary File.
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
Convert Temporary file to URI and invoke camera activity intent.
static final int REQUEST_TAKE_PHOTO = 1;
Uri photoURI;
File photoFile;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
...
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
Now you already have File location path created before Camera Activity intent called. You can handle RESULT_OK(Show image) or RESULT_CANCELED(Delete temporary image).
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
mImageView.setImageURI(photoURI );
}else{
photoFile.delete();
}
}
*Take note on getUriForFile which returns a content:// URI. For more recent apps targeting Android 7.0 (API level 24) and higher, passing a file:// URI across a package boundary causes a FileUriExposedException. Therefore, we now present a more generic way of storing images using a FileProvider.
Full documentation is here for reference.
Taking Photo Simply

Get Image From Camera result null in data onActivityResult

I want to get image from camera, using
public void LicenseCameraIntent() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, PROFILE_REQUEST_CAMERA);
}
Return bitmap, but it works perfectly. Unfortunately image show as a really small size.
So, i have to keep searching until i get
public void licenseCameraIntent() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri mImageCaptureUri1 = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri1);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, LICENSE_REQUEST_CAMERA);
}
On OnActivityResult data always show null result. How I can fix this issue. It is possible using first solution but return bigger image?
Thanks
while you are capture image from the camera you have created a file for an image that is your image full path so make it globally.
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.i(TAG, "IOException");
}
// Continue only if the File was successfully created
if (photoFile != null) {
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
and the image file, mCurrentPhotoPath is the full path of image
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, // prefix
".jpg", // suffix
storageDir // directory
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
after onActivityResult() you get the image from mCurrentPhotoPath
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
mImageView.setImageBitmap(mImageBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
For more info, you can check this link Capture Image from Camera and Display in Activity
try this,
public static int REQUEST_CAMERA = 111;
private void cameraIntent() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
code for onStartActivityForResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
_FileName = "image file";
capturePic = null;
file_pdf = null;
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
//mImageView.setImageBitmap(imageBitmap);
img_ticket_detail.setImageBitmap(imageBitmap);
capturePic = imageBitmap;
}
}
}
also you can use glide library for show image

How can I take a photo from Camera with full size? (Android Java)

I can't take a picture from the camera and then store it as a Bitmap object. I can only find solutions where I get thumbnail as Bitmap. How can I do this?
Here is the code:
public boolean pickImageFromCamera(View View) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo;
try
{
// place where to store camera taken picture
photo = this.createTemporaryFile("picture", ".jpg");
photo.delete();
}
catch(Exception e)
{
System.out.println("ERROR TAKING PICTURE");
return false;
}
mImageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
//start camera intent
activity.startActivityForResult(intent, REQUEST_CODE2);
return true;
}
private File createTemporaryFile(String part, String ext) throws Exception
{
File tempDir= Environment.getExternalStorageDirectory();
tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
if(!tempDir.exists())
{
tempDir.mkdirs();
}
return File.createTempFile(part, ext, tempDir);
}
public void pickImageFromFolder(View View) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , REQUEST_CODE);//one can be replaced with any action code
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE2 && resultCode == Activity.RESULT_OK)
try {
// We need to recyle unused bitmaps
if (bitmap != null) {
bitmap.recycle();
}
Bitmap bitmap = null;
this.getContentResolver().notifyChange(mImageUri, null);
ContentResolver cr = this.getContentResolver();
try
{
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
imageView.setImageBitmap(bitmap);
}
catch (Exception e)
{
System.out.println("Failed to load");
}
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
this.UPLOAD_URL = Config.webSiteUrl + "?action=uploadFile&username=" + this.username + "&password=" + this.password + "&baustelleid=" + Fotos.this.baustelleid;
if(bitmap != null) {
uploadImage(bitmap, this.UPLOAD_URL);
}
} catch (Exception e) {
System.out.println("Fehler 1");
}
super.onActivityResult(requestCode, resultCode, data);
}
It ends up with System.out.println("ERROR TAKING PICTURE"); It seems like there is no permission to write the storage. How can I change this?
Call this method in button etc. after that get bitmap onActivityResult
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
File photoThumbnailFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(this,
"com.example.yourfileprovider",
photoFile);
photoThumbnailURI = FileProvider.getUriForFile(this,
"com.example.yourfileprovider",
photoThumbnailFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
This method will Create file
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
This method will happen when the camera is done taking the picture
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Toast.makeText(this, "Image saved", Toast.LENGTH_SHORT).show();
bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoURI);
mImageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
You will get a bitmap, but you can get the whole one, not just the thumbnail.
Have you tried this answer ? https://stackoverflow.com/a/6449092/4127441

MediaStore.ACTION_VIDEO_CAPTURE not saving the video in Nougat 7.0

Hi i have been trying to record and save a video from Nougat 7.0 using intent, I can record a video but its not getting saved in the device storage. I even used FileProvider to avoid 'FileUriExposedException'. But when it comes for Capturing Images it is getting saved in the below specified path.
Here is my code.
private Uri imageUri;
private File imageFile = null;
public File videoFilePath() {
return new File(getDefaultCameraPath(), "Video_" + System.currentTimeMillis() + ".mp4");
}
private void callCameraIntent(){
Intent cameraIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
imageFile = videoFilePath();
imageUri = FileProvider.getUriForFile(CreatePostActivity.this, BuildConfig.APPLICATION_ID + ".provider", imageFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, 2);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_CANCELED) {
//Here it returns imageFile does not exist so it skips the if statement
if (resultCode == Activity.RESULT_OK && requestCode == 2 && imageFile != null && imageFile.exists()) {
}
}
}
The above code works well for all the pre-Nougat version. Can anyone provide me a better solution to record the video and save in device storage.
here is the solution !
If your targetSdkVersion is 24 or higher, we have to use FileProvider class to give access to the particular file or folder to make them accessible for other apps.
Stackoverflow link:
android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()
also you should check the required permission android.Manifest.permission.CAMERA
File Video_folder = new File(Environment.getExternalStorageDirectory(),
"Sample/Videos")
File videoMediaFile=null;
private void DispatchVideoRecordIntent() {
try {
videoMediaFile = File.createTempFile(
"VID_" + System.currentTimeMillis(),
".mp4",
Video_folder
);
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videoMediaFile));
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
} catch (Exception e) {
Log.e("Image Capturing", e.toString());
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQUEST_VIDEO_CAPTURE) {
if (!TextUtils.isEmpty(videoMediaFile.getAbsolutePath())) {
// videoMediaFile.getAbsolutePath() is your file path
} else {
Toast.makeText(this, "Unable to capture video.", Toast.LENGTH_SHORT).show();
}
}

Camera Intent not returning to calling Activity

I have read a lot of questions regarding this problem. Some are similar to what I am experiencing; I have tried the answers with no success. The code works on a HTC Android 4.2 and doesn't work on a Nexus 7 Android 4.4. I have modified the storage directory to work on android 4.4, so this isn't the problem.
The camera intent never returns if I use
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
and does return if I use
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFile);
but it doesn't save the file.
Here is the full code.
Call the intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(manager) != null)
{
try
{
final File photoFile = createImageFile();
if (photoFile != null)
{
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
//takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFile);
startActivityForResult(takePictureIntent, 1);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
The file name
private File createImageFile() throws IOException
{
if (mStorageDirectory == null)
{
createInitialStorageDirectory();
setupFolders();
mScrollList.notifyDataSetInvalidated();
}
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "slide_" + timeStamp;
File storageDir = new File(mStorageDirectory);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
// image.setWritable(true, false);
// Save a path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
The callback function
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == -1)
{
}
}
The root directory plus the save directory
static String mBasePath = "/slides/";
private String getRootDirectory()
{
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{
if (Build.VERSION.SDK_INT >= 19)
return mView.getContext().getFilesDir() + mBasePath;
else
return Environment.getExternalStorageDirectory() + "/Android/data/com.hdms.manager" + mBasePath;
//return Environment.getExternalStorageDirectory() + mBasePath;
}
return mBasePath;
}
Any thoughts would be appreciated.
In my case, it did not return because the output file was inside a folder which I have not created yet.
If it is the case for someone else, do this before starting anintent:
file.getParentFile().mkdirs();
So I have found a solution to my problem. If a file doesn't exist then the Camera Activity will not return. I guess the reason that it doesn't work on 4.4 is the change to the files system. I am not saving the image to the media gallery and loading the file back into my apps directory. The media file is then deleted. The reason that I don't leave it in the media directory, is when the app is deleted so will the images be deleted.
Here is the new code. First the intent call
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(manager) != null)
{
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION,"From your Camera");
Uri mImageUri = App.mApplication.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
mCurrentPhotoPath = mImageUri.getPath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(takePictureIntent, 1);
}
Then the callback.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == -1)
{
if (data != null)
mCurrentPhotoPath = String.valueOf(data.getData());
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = App.mApplication.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, filePathColumn, null, null, null);
if (cursor == null)
return;
// find the file in the media area
cursor.moveToLast();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
File source = new File(filePath);
cursor.close();
// create the destination file
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "slide_" + timeStamp;
File destination = new File(mStorageDirectory, imageFileName + ".jpg");
// copy the data
if (source.exists())
{
try
{
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
source.delete();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
In our scenario our app is able to launch the camera app either via a 'button' click or an 'imageview' click. When using the button route the camera app correctly returned to the calling Activity as expected, however when tapping the imageview the results were inconsistent - sometimes it would return and sometimes the back button had to be tapped several times.
Turns out the solution was my oversight: I was not removing the event handler for the imageview in the OnPause method - once I did then it worked perfectly.

Categories

Resources