get current picture folder of camera - android

I have a small FileExplorer in my app and i want him to start in the folder, which is currently used by the defautl camera. Is there a way to get this path?
I tryed:
Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath());
But this do not returns "/mnt/sdcard/Pictures" and my Camera is storing in "mnt/sdcard/ext_sd/DCIM/100MEDIA/"
PS:
I do know how to start the camera with a specific folder for storing the pictures, that's not what i'm searching for,

String[] projection = new String[]{MediaStore.Images.ImageColumns._ID,MediaStore.Images.ImageColumns.DATA,MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,MediaStore.Images.ImageColumns.DATE_TAKEN,MediaStore.Images.ImageColumns.MIME_TYPE};
final Cursor cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,projection, null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
if(cursor != null){
cursor.moveToFirst();
// you will find the last taken picture here
// according to Bojan Radivojevic Bomber comment do not close the cursor (he is right ^^)
//cursor.close();
}

Related

Set last taken image automatically form gallery in image view

please check image hereI need to set last taken image form gallery in image view or on Image button automatically just like used in default camera ,some body help me to sort out this problem
We can get the last taken image from MediaStore,
String[] proj = new String[]{
MediaStore.Images.ImageColumns.DATA,
};
final Cursor cursor = getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, null,
null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
if (cursor.moveToFirst()) {
String lastImageLocation = cursor.getString(0);
}

Apache FileUtils.MoveFile does not delete image after its been moved, device restart is required for the update to take place

I'm using org.apache.commons.io.FileUtils to move files from one directory to another, like so:
File source = new File("/my/image/directory/image.jpg";
File destination = new File("/new/image/directory/image.jpg");
FileUtils.moveFile(source, destination);
Note that the source directory is an external directory and destination is an internal directory.
After above code is executed, I query to get the files of my internal directory like so:
File vaultDir = ctx.getDir("dir_name", Context.MODE_PRIVATE);
String[] fileList = vaultDir.list();
//Iterate and print file names
This is working as intended. But when i query the MediaStore like so:
ArrayList<GridViewObject> objects = new ArrayList<>();
String[] mProjection = {MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.DATA};
ContentResolver cr = ctx.getContentResolver();
Cursor mCursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
mProjection,
null,
null,
MediaStore.Images.Media.DEFAULT_SORT_ORDER);
mCursor.moveToFirst();
while (!mCursor.isAfterLast()) {
GridViewObject tmpGridViewObject = new GridViewObject();
tmpGridViewObject.setTitle(mCursor.getString(0));
tmpGridViewObject.setUrl(mCursor.getString(1));
objects.add(tmpGridViewObject);
mCursor.moveToNext();
}
The image is still returned to me by the query. Also, when I open my camera gallery app, the image is still there, as if it was never deleted.
I noticed that if I restart the phone, the image disappears from my device camera gallery app as intended (and above query return correct result). So I think it might be an indexing problem.
What do I need to do to "update" the MediaStore to show the intended result?
I solved this by instead of deleting the file using FileUtils.delete(srcFile, destFile); i copied the the file using FileUtils.copy(srcFile, destFile); and then to refresh MediaStore i used the following code to delete the source entry:
String selection = MediaStore.Images.Media.DATA + "='" + source.getAbsolutePath() + "'";
ctx.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, null);
Credit goes to Pascal's answer to this question.

Detect Android Camera folder

I would like to detect the camera folder on every Android device. From what I've read this folder differs from an manufacturer to another and there is no guarantee that there will be even an DCIM folder on the device.
This is the method that I'm using to get the files now:
private static final Set<String> FILTER_FOLDERS = new HashSet<String>(
Arrays.asList(new String[] { "camera", "100andro", "100media" }));
private Set<String> getCameraPictures() {
final String[] columns = new String[] {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.MIME_TYPE };
// Order by options - by date & descending
final String orderBy = MediaStore.Images.ImageColumns.DATE_TAKEN
+ " DESC";
// Stores all the images from the gallery in Cursor
final Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // base URI for
// the Images
columns, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
orderBy); // Ordering
// Total number of images
int count = cursor.getCount();
// Create an array to store path to all the images
String[] picturesPath = new String[count];
if (cursor.moveToFirst()) {
int dataColumn = cursor
.getColumnIndex(MediaStore.Images.Media.DATA);
int bucketColumn = cursor
.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
do {
if (FILTER_FOLDERS.contains(cursor.getString(bucketColumn)
.toLowerCase(Locale.getDefault()))) {
// Store the path of the image
picturesPath[cursor.getPosition()] = cursor
.getString(dataColumn);
}
} while (cursor.moveToNext());
}
// Close the cursor
if (null != cursor) {
cursor.close();
}
return new HashSet<String>(Arrays.asList(picturesPath));
}
But this is returning images from other places also ...
How can I retrieve only the images taken with the camera ?
If there is no native way to do this, where can I find what are the names for the folders used by each manufacturer (as many as there are) so that I can filter it by BUCKET_DISPLAY_NAME ?
Thank you
LE:
I have updated the method to get the images on device & also filter the folders.
There are dozens, perhaps hundreds, of camera apps that ship with devices, to go along with thousands of camera apps available for download. None have to use a particular "camera folder" and none have to have their images indexed by MediaStore.
The conventional "camera folder" for a device will be in the location specified by Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM). That directory might not exist yet, if no camera app has used it. But, again, there is no requirement that a camera app use it -- they can store their images wherever they want to, including places that you cannot access (e.g., internal storage, "the cloud").
How can I retrieve only the images taken with the camera ?
You can't. There are well over one billion smartphones on the planet, and any phone could have pictures on it taken by any camera from any other phone, courtesy of photo-sharing apps and sites. This is on top of pictures taken by cameras other than smartphones. There is no requirement that images taken by the device's own camera need to be somehow designated as such for your benefit.

Android ContentResolver.query always returns same data

I have videoplayer app with filebrowser listing all videos on SD card
Code inspired by i want get audio files in sd card
Using ContentResolver, works as expected, but it does not update if the files on card change. I do not mean automatically, but after view/app restart. Not even reinstalling the application helped, still shows the same files. The deleted video file is not visible via PC nor it is possible to play it (This video cannot be played (translation)).
I dumped the data and the problem is not in view caching or elsewhere. I do not implement any caching of my own and failed to find anything on the matter. Thank you
Code:
// acquisition
String[] projection = {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.DATA
};
ContentResolver resolver = getActivity().getContentResolver();
Cursor videoCursor = resolver.query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
null
);
// extraction
while(cursor.moveToNext()) {
cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
filepath = cursor.getString(cursorIndex);
cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
filename = cursor.getString(cursorIndex);
cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
duration = cursor.getString(cursorIndex);
result[ index++ ] = new VideoFileMetadata(filename, duration, filepath);
}
Edit 1 [14-03-2013]:
I tried adding number + " = " + number to ORDER or WHERE clause to act as a potential query caching buster, but it had no effect (although it's possible it was removed by an optimizer as a useless clause). This time I had reinstalled the application from a different machine using different certificate, but the query result remained the same, listing currently non-existing files.
You should first call cursor.moveToFirst() .
So, your cursor iteration loop should look like
if (cursor.moveToFirst()) {
do {
// cursorIndex = cursor.getColumnIndexOrThrow, etc...
} while (cursor.moveToNext());
}

Image disappears in Android Gallery when renamed

I am trying to rename an image in Gallery taken from Camera to give a new name. When I renamed the file the original image disappears showing 2 black squares . Can any one help me in sorting this issue by telling me procedure to rename Gallery Image.
Path is as follows : /mnt/sdcard/DCIM/Camera
I tried to update the Image title through content provider as below:
ContentValues val = new ContentValues();
val.put(Images.Media.TITLE, "ImageTitle23");
getContentResolver().update(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, val, null, null);
The image title is changed in Gallery.
But when I try to query the updated image as below I am getting the name same as previous one :
final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, MediaStore.Images.Media._ID+"="+id, null, imageOrderBy);
if(imageCursor.moveToFirst()){
int idd = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
Can anyone help me in sorting out this issue?
The MediaStore looks for changes in your pictures BUT it will not react to all changes in real time. Each time you do such a modification, you should ask the MediaScanner to scan this file:
MediaScannerConnection.scanFile(
getApplicationContext(),
new String[]{file.getAbsolutePath()},
null,
new OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Log.v("grokkingandroid",
"file " + path + " was scanned seccessfully: " + uri);
}
});
Also, you should check this blog post that gives a lot of useful informations on this usecase.
The Gallery is interacting with the MediaStore content provider. If you change the name of the image file you probably need to update the field in the content provider.
You can read up on it in the MediaStore reference : http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html
Potentially you can just trigger a refresh of the MediaStore. Look at this thread : How can I refresh MediaStore on Android?

Categories

Resources