Android saving image in default folder with default image name - android

my application is making photos and viewing them in ImageView.
Everything works fine, but images I make in my application are saved in folder DCIM/CAMERA/ with names like "1369434756474" or "1369920366597".
I would like to save images like original camera in default folder DCIM/100MSDCF with default names like "DSC00013" or DSC00233".
I am using Sony Xperia X10Mini but I would like my app worked fine on all devices.
Below is my code of requesting image capture:
if (isImageCatchingIntentAvailable()){
String fileName = "photo.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mImageCaptureUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(intent, MAKE_PHOTO);
}

Related

Android Camera intent with Uri not saving the images on Camera folder

I am using Camera Intent with following code
public void clickPicturesThroughCamera() {
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
initImageUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, RETURN_FROM_CAMERA);
} catch (Exception e) {
showToast(getString(R.string.error_opening_camera));
}
}
public void initImageUri() {
ContentValues cv = new ContentValues();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat,
Locale.ENGLISH);
String name =simpleDateFormat.format(Calendar.getInstance().getTime())
+ "_" + new Random().nextInt(100) + ".jpg";
cv.put(MediaStore.Images.Media.TITLE, name);
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
}
The images captured will be stored on /storage/sdcard0/DCIM/Camera/imagename.jpg. But when I click images through default Camera app it will be stored on storage/extSdCard/DCIM/Camera/imagename.jpg as I have used external sdcard to store the the captured images by default.
So my main requirment is that the app I am developing should store the captured images on the default location i.e. storage/extSdCard/DCIM/Camera/imagename.jpg instead of /storage/sdcard0/DCIM/Camera/imagename.jpg. For that what should I do in the above code, so that it will always save the images on default Camera folder.
Thanks
The path can vary among devices. By using MediaStore.Images.Media.EXTERNAL_CONTENT_URI you are asking the device for the correct path.
http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html

Getting images from a particular folder

I am trying to save the image in the folder which is captured using camera.The image does get saved in the particular folder but there is another copy of the same image in the "Camera" folder.This happens only in Android Version 2.2.1.
When I tried the same case in android version 4.1.2, there is only one copy saved in the particular folder and not in "Camera" folder.
This is the code I am using to start the camera and save the image in "MyFolder".
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File storagePath = new
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
+ "/MyFolder/");
if(!storagePath.isDirectory()){
storagePath.mkdirs();
}
File myImage = new File(storagePath,Long.toString(System.currentTimeMillis()) + ".jpg");
Uri fromURI=Uri.fromFile(myImage);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fromURI);
startActivityForResult(intent,CAMERA_REQUEST_CODE);
After saving the image, I will create a custom Gallery view where I will try to get all the images so that the user can select any one of the image.when I try getting images, the new image which is taken from the camera is not retrieved in the cursor.
This is the code I am using to get all the images...
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID+" DESC";
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,null,null, orderBy);
In Android V 2.2.1, since the copy of the image is saved in "Camera" folder, I get the new image, but in Android V 4.1.2, I don't get the image in the cursor.
Does anyone know how to get the image from a particular folder??
TIA,
VijayRaj

Android ContentResolver.insert crashes with "unable to create new file"

I have a code inside some function of my activity:
ContentValues cv = new ContentValues();
cv.put(MediaStore.Images.Media.TITLE, "1354213408296.jpg");
ContentResolver contentResolver = getContentResolver();
Uri imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(cameraIntent, 712984419/*Some request code*/);
It crashes with:
java.lang.IllegalStateException: Unable to create new file:
/mnt/sdcard/DCIM/Camera/1354213408296.jpg at
android.os.Parcel.readException(Parcel.java:1335) at
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:182) at
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136) at
android.content.ContentProviderProxy.insert(ContentProviderNative.java:415) at
android.content.ContentResolver.insert(ContentResolver.java:730)
crashes on:
contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
The "1354213408296.jpg" is just System.currentTimeInMillis() + ".jpg", so it is always unique
The android.permission.WRITE_EXTERNAL_STORAGE is provided in manifest
Here is some phone's environment specifications (I am using ACRA to get it):
getDataDirectory=/data
getDownloadCacheDirectory=/cache
getExternalStorageAndroidDataDir=/mnt/sdcard/Android/data
getExternalStorageDirectory=/mnt/sdcard
getExternalStorageState=removed
getRootDirectory=/system
getSecureDataDirectory=/data
getSystemSecureDirectory=/data/system
is4GConfig=true
is8GConfig=false
isEncryptedFilesystemEnabled=false
isExternalStorageEmulated=false
isExternalStorageRemovable=true
What can I do to prevent this crashes?
I'm not sure what you're trying to do. All you seem to be doing is trying to create a new row in MediaStore.Images.Media, with only a TITLE column. Putting in a title without the data to go with it doesn't make much sense.
This seems to be just another exception you will get when no sdcard is present (I was able to reproduce it only on very weird emulators, but who knows?). Cases of missing sdcard should be handled for sure. My current solution is as follows:
public static Uri getImageFileUri(Context context) throws IOException{
String fullFileName = generateImageFileName(imageName); // a method i have defined
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fullFileName);
return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return Uri.fromFile(new File(fullFileName));
}
}
This is the method I use to generate the Uri I will start the camera intent with. Afterwards I use it exactly like you do. The thing is that the value I return in the no sdcard case will not work properly, but on the other hand Android devices do not allow taking pictures if no sdcard is present. Using this solution you will succeed in taking picture if there is a sdcard and will launch the native camera that will show message "Insert sdcard in order to take picture" in the other cases.

Android ACTION_IMAGE_CAPTURE with EXTRA_OUTPUT in internal memory

When I'm taking a photo from a camera if I'm calling
File file = new File(getFilesDir().getAbsolutePath() + "/myImage.jpg");
Uri outputFileUri = Uri.fromFile(file);
cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
OK button on camera app is not functioning, simply does nothing (actually won't save it to internal memory I provided I guess and therefore the app itself does nothing).
If I however call
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myImage.jpg");
Uri outputFileUri = Uri.fromFile(file);
cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
everything is fine and photo is stored on SDCard.
My question is, is there a way to store capture photo in full-resolution without SDCard?
The native camera app cannot save the image in your app's private internal directories as those are only available to your particular app.
Instead you can create a custom camera activity to save images to your internal directories or you need to use the stock camera app with external storage.
Note: if you plan on creating a custom camera activity make sure you target at least 2.3 and up. Anything below that mark is very difficult to work with.
The Camera activity will not be able to save the file into your activity's private files directory, that's why it fails quietly. You can move the image from the external storage into your files dir in onActivityResult.
You need to add permission
cameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
This is possible using the file provider. Please refer sample
public getOutputUri(#NonNull Context pContext) {
String photo = photo.jpeg;//your file name
File photoFile = new File(pContext.getFilesDir(), photo);
Uri lProviderPath = FileProvider.getUriForFile(pContext,
pContext.getApplicationContext()
.getPackageName() + ".provider", photoFile);
return lProviderPath;
}
private void capturePhoto() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getOutputUri(this));
cameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(cameraIntent, 1);
}
Refer following android document for more details
https://developer.android.com/reference/android/support/v4/content/FileProvider

How to save an image to the media provider in Android?

I used this code snippet:
ContentValues image = new ContentValues();
image.put(Images.Media.TITLE, "ImageTitle");
image.put(Images.Media.DISPLAY_NAME, "Heart");
image.put(Images.Media.DESCRIPTION, "Heart");
image.put(Images.Media.DATE_ADDED, dateTaken);
image.put(Images.Media.DATE_TAKEN, dateTaken);
image.put(Images.Media.DATE_MODIFIED, dateTaken);
image.put(Images.Media.MIME_TYPE, "image/png");
image.put(Images.Media.ORIENTATION, 0);
File parent = imageFile.getParentFile();
String path = parent.toString().toLowerCase();
String name = parent.getName().toLowerCase();
image.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
image.put(Images.Media.SIZE, imageFile.length());
image.put("_data", imageFile.getAbsolutePath());
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
I create the image inside the cache directory of my application. I assume that the image is not copied into the media folder by the media provider and therefore not accessible from the media gallery application. Is it possible to add an image to the media gallery without writing the file to the mass storage of the phone?
I think that the answer you are looking for is described here. The second part of that section describes saving images. The gist of it appears to be that you first insert the ContentValues object into the content resolver, which returns a Uri object that the ContentResolver is willing to turn into an OutputStream for you that you can write the image to. The link provided includes the actual code you would need to do that.

Categories

Resources