how to obtain picture in camera with MediaStore - android

I try to use MediaStore to get picutures that stored in SDcard:DCIM/Camera.my code is like this:
Cursor track = mContext.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);
but I get all pictures that in SDcards,I need get only in DCIM/Camera,help me please.

Try following code.
String SD_CARD_CAM_DIR = Environment.getExternalStorageDirectory() + File.separator + "DCIM" + File.separator + "Camera";
File file =new File(SD_CARD_CAM_DIR);
Cursor track = mContext.getContentResolver().query(Uri.fromFile(file), null, null,null,null);

Related

Android Q: How to get a list of images from a specific directory

Android Q: I need to get a list of images from a specific directory I saved images on it and display these images on my app.
Save images code:
final String relativeLocation = Environment.DIRECTORY_PICTURES + File.separator + "MyMedia" + File.separator + "Photo";
ContentResolver resolver = context.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
bmp.compress(Bitmap.CompressFormat.JPEG,100, fos);
Objects.requireNonNull(fos).close();
Objects.requireNonNull(fos).flush();
Get images code:
Uri externalUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {
MediaStore.Files.FileColumns._ID,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.MediaColumns.TITLE,
MediaStore.Images.Media.MIME_TYPE,
MediaStore.MediaColumns.RELATIVE_PATH
};
Cursor cursor = context.getContentResolver().query(externalUri, projection, null, null, MediaStore.Images.Media.DATE_TAKEN +" DESC");
int idColumn = cursor.getColumnIndex(MediaStore.MediaColumns._ID);
int titleColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE);
int relativePathColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.RELATIVE_PATH);
while (cursor.moveToNext()) {
Uri photoUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getString(idColumn));
}
I need a direct query to get list of all images saved in the RELATIVE_PATH , ("MyMedia/Photo") without add, if condition in the cursor loop to check relativePathColumn
if equal "MyMedia/Photo", because of this loop for all images in the device of the user!
Did we have any way to get a list of images directly from my directory?
String path = "MyMedia/Photo";
String selection = MediaStore.Files.FileColumns.RELATIVE_PATH + " like ? " ;
String selectionargs []= new String[]{"%" + path + "%"};
.query(externalUri, projection, selection, selectionars, MediaStore.Images.Media.DATE_TAKEN

Retrieve Images from Certain Folder in Android

I have the following code where i am getting all the images from Camera but I want it from a certain folder as Pictures/SanPics. This folder is already created and has some images but it always returns NullPointerException.
CODE :
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Pictures" + File.separator + "SanPics";
File file = new File(path);
Uri myUri = Uri.fromFile(file);
Cursor imagecursor = getContentResolver().query(myUri, columns, orderBy, selectionArgs,null);
int image_column_index = imagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
Help will be much appreciated. Thanks in advance.

Access ordered images and video in same Cursor

I'm using the android.content.CursorLoader class to create two Cursor objects to access media stored on the user of my app's device. I'd like to give the user a grid view of their stored images and video which preserves order from the Android Gallery app.
Currently I'm using one Cursor to access Images and one to access Video. With this approach, all images precede all videos (i.e. they are in two separate groups). Is there a way to access both Images and Video from the same Cursor? If not, is there a better way to access these media on the device?
For reference, here is the code I am using:
For Images:
CursorLoader cursorLoader = new CursorLoader(
mContext,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
IMAGE_PROJECTION,
null,
null,
MediaStore.Images.Media._ID + " desc"
);
mImageCursor = cursorLoader.loadInBackground();
And Video:
CursorLoader cursorLoader = new CursorLoader(
mContext,
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
VIDEO_PROJECTION,
null,
null,
MediaStore.Video.Media._ID + " desc"
);
mVideoCursor = cursorLoader.loadInBackground();
After lots of research and playing around with source code, I'm finally a bit more familiar with the Android filesystem. To get a single Cursor which can access information about both Images and Video I used the following:
// Get relevant columns for use later.
String[] projection = {
MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns.MEDIA_TYPE,
MediaStore.Files.FileColumns.MIME_TYPE,
MediaStore.Files.FileColumns.TITLE
};
// Return only video and image metadata.
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
+ " OR "
+ MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;
Uri queryUri = MediaStore.Files.getContentUri("external");
CursorLoader cursorLoader = new CursorLoader(
this,
queryUri,
projection,
selection,
null, // Selection args (none).
MediaStore.Files.FileColumns.DATE_ADDED + " DESC" // Sort order.
);
Cursor cursor = cursorLoader.loadInBackground();

how update Android MediaStore after i move a JPG file

my app move .jpg file to others folders and to get viewable in the stock gallery i have sendBroadcast ACTION_MEDIA_MOUNTED
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" +
Environment.getExternalStorageDirectory() )));
but this take much time.. i have understand (maybe) that i have to update manually with cursor/contentResolver in mediaStore directly to get this faster. can anyone help me on this? thanks..
my code actually is:
Uri uri = (Uri) list.get(cont);
Cursor cursor = managedQuery(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String app = cursor.getString(column_index);
File orig = new File( app.toString());
File dest = new File( destination_path +"/"+ orig.getName().toString());
orig.renameTo(dest);
with this i move a file from a path to another one.
after this, to get images in gallery
i have to sendBroadcast ACTION_MEDIA_MOUNTED
I just had to do the same, use following to update the MediaStore:
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, newPath);
boolean successMediaStore = context.getContentResolver().update(
MediaStore.<TYPE>.Media.EXTERNAL_CONTENT_URI, values,
MediaStore.MediaColumns.DATA + "='" + oldPath + "'", null) == 1;
Replace <TYPE> with the correct media store... (Images, Video, Audio...)
Use the MediaScannerConnection to update the OS:
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
// code to execute when scanning is complete
}
});

Change uri in cursor to load images from local dierctory

I downloaded example where gallery loaded images like
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
columns, null, null, orderBy);
When I print MediaStore.Images.Media.EXTERNAL_CONTENT_URI I get:
content://media/external/images/media .
But how can I change to load from directory like:
Environment.getExternalStorageDirectory() + "/test/";
I tried
Uri uri=Uri.parse(Environment.getExternalStorageDirectory() + "/test/");
and
Uri uri=Uri.parse("content:"Environment.getExternalStorageDirectory() + "/test/");
but it doesn't work. Can anybody help me?
Try like this,
File ff = new File("/sdcard/test");
Cursor imagecursor = managedQuery(Uri.fromFile(ff), columns, null, null, orderBy);

Categories

Resources