Android - Taking Photos and Saving Them with a Custom Name to a Custom Destination via Intent

Android - Taking photos/video and saving them with a custom name to a custom destination via Intent work on some devices only why?

I had the same issue that in some phone it was working and for others doesn't, I found my solution in this link I hope this help you:

Deleting a gallery image after camera intent photo taken

Android - Taking photos and saving them with a custom name to a custom destination via Intent

Here's the code that made it work:

//camera stuff
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

//folder stuff
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();

File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);

imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

It opens the camera and takes exactly one shot (it goes back to the main activity after the user saves the image that was taken. It saves the image to the specified folder.

Android - Taking photos and saving them with a custom name to a custom destination via Intent

Here's the code that made it work:

//camera stuff
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

//folder stuff
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();

File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);

imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

It opens the camera and takes exactly one shot (it goes back to the main activity after the user saves the image that was taken. It saves the image to the specified folder.

Android Camera Intent on Glass - Save image in custom file path

The file may not be completely written when you look at it, so use a FileWatcher like the one in the GDK docs:

private static final int TAKE_PICTURE_REQUEST = 1;

private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
String picturePath = data.getStringExtra(
CameraManager.EXTRA_PICTURE_FILE_PATH);
processPictureWhenReady(picturePath);
}

super.onActivityResult(requestCode, resultCode, data);
}

private void processPictureWhenReady(final String picturePath) {
final File pictureFile = new File(picturePath);

if (pictureFile.exists()) {
// The picture is ready; process it.
} else {
// The file does not exist yet. Before starting the file observer, you
// can update your UI to let the user know that the application is
// waiting for the picture (for example, by displaying the thumbnail
// image and a progress indicator).

final File parentDirectory = pictureFile.getParentFile();
FileObserver observer = new FileObserver(parentDirectory.getPath()) {
// Protect against additional pending events after CLOSE_WRITE is
// handled.
private boolean isFileWritten;

@Override
public void onEvent(int event, String path) {
if (!isFileWritten) {
// For safety, make sure that the file that was created in
// the directory is actually the one that we're expecting.
File affectedFile = new File(parentDirectory, path);
isFileWritten = (event == FileObserver.CLOSE_WRITE
&& affectedFile.equals(pictureFile));

if (isFileWritten) {
stopWatching();

// Now that the file is ready, recursively call
// processPictureWhenReady again (on the UI thread).
runOnUiThread(new Runnable() {
@Override
public void run() {
processPictureWhenReady(picturePath);
}
});
}
}
}
};
observer.startWatching();
}
}

Saving Camera Intent Image To Internal Data Custom Directory

So this is how I solved it. Thanks goes to @AppMobiGurmeet also.

Save the photo to a file on the SDCard like below.

        File directory= new File(Environment.getExternalStorageDirectory()+File.separator+"MyApplication"+File.separator+"TemporaryImages");//+File.separator+CCPhotoManager.getInstance().getNewPhotoFileName()+CCPhotoManager.JPEG_FILE_SUFFIX);
directory.mkdirs();

File externalFile = new File(Environment.getExternalStorageDirectory()+File.separator+"MyApplication"+File.separator+"TemporaryImages"+File.separator+CCPhotoManager.getInstance().getNewPhotoFileName()+CCPhotoManager.JPEG_FILE_SUFFIX);

Uri uriSavedImage=Uri.fromFile(externalFile);
Log.d("PhotoManager", "adding extra to intent: "+uriSavedImage.toString());
Intent launchcameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
launchcameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(launchcameraIntent,CAMERA_PIC_REQUEST);

Then on activity result. (when the intent is returned)

            // SAVE THE PHOTO TO THE INTERNAL DATA DIRECTORY
File to = new File(MyApplication.getAppContext().getFilesDir()+File.separator+"MyApplication"+File.separator+DIRECTORY+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX);
File from = new File(Environment.getExternalStorageDirectory()+File.separator+"MyApplication"+File.separator+"TemporaryImages"+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX);

try {
FileChannel destination = new FileOutputStream(to).getChannel();
FileChannel source = new FileInputStream(from).getChannel();
if (destination != null && source != null) {
destination.transferFrom(source,0,source.size());
from.delete();
}
destination.close();
source.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

I have now decided however, after all this faffing around, that the internal data directory is not the best place to store things like Images and Files due to, on some devices, there being a very small amount of internal space (Samsung Galaxy Ace approx 200MB).

I hope this helps someone.

Android using build-in camera to capture multi-photos?

Each time a user takes a photo you must save it (under "onActivityResult()") with a different name so the last capture does now overwrite the previous. One easy solution is to concatenate in the photo name a timestamp so the photo name will become unique and it will be saved as a new photo.

If you want to get the list of those photos saved, you can use this example "android-coding.blogspot.gr/2011/10/list-filesdirectory-in-android.html"

Why it's so hard taking photos normally on different Android phones?

The way that @balban shah offered worked all the same when I tried.

Finally I found that's because different manufacturers customize their Rom, including the Camera app, so the best way is not to call the default camera app, instead we can write a activity use hardware.camera to take picture. There are a lot of examples for this around the Internet too.



Related Topics



Leave a reply



Submit