Retrieve Images from Certain Folder in Android - 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.

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

Why can't I see some of my pictures in the gallery?

I'm trying to read photos existing in the sdcard with MediaStore.Images.Media.DATA but I'm always getting an empty cursor !!!
This is the code I'm using for the reading :
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
Cursor imagecursor = getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
MediaStore.Images.Media.DATA + " like ? ",
new String[] { "%"+eventName.trim()+"%" }, null);
imagecursor.setNotificationUri(getContentResolver(),
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
this.imageUrls = new ArrayList<String>();
Log.i("imagecursor.getCount()", Integer.toString(imagecursor.getCount()));
for (int i = 0; i < imagecursor.getCount(); i++) {
imagecursor.moveToPosition(i);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
imageUrls.add(imagecursor.getString(dataColumnIndex));
}
Of course I added this
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
to the AndroidManifest.xml
I found that the problem was with the Media Scanner so i solved it buy using a simple app called Scan Media witch scan the external storage and then the MediaStore will be able to find the new elements .
i donĀ“t know if this will help you but I is working me
String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/yoururl/";
Toast.makeText(getActivity(), targetPath, Toast.LENGTH_LONG).show();
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files){
myImageAdapter.add(file.getAbsolutePath());
}
in this example i get images and show in a grid view

how to obtain picture in camera with MediaStore

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);

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
}
});

Image is saved twice

I am using the following intent:
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
Basically i am using ACTION_IMAGE_CAPTURE intent to invoke the camera and save the taken image into the specified uri.
It works but at the same time the image is also saved with the default name.
Thus once i have snapped the picture, it is saved twice, both in the uri and in the default path and name.
How do i ensure that it is only saved in the specified uri?
Thanks In Advance,
Perumal
You can take the ID or Absolute path of the gallery last image. And delete it.
It can be done like that:
/**
* Gets the last image id from the media store
* #return
*/
private int getLastImageId(){
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, null, null, imageOrderBy);
if(imageCursor.moveToFirst()){
int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
Log.d(TAG, "getLastImageId::id " + id);
Log.d(TAG, "getLastImageId::path " + fullPath);
imageCursor.close();
return id;
}else{
return 0;
}
}
And to remove the file:
private void removeImage(int id) {
ContentResolver cr = getContentResolver();
cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media._ID + "=?", new String[]{ Long.toString(id) } );
}
This code was based on the post: Deleting a gallery image after camera intent photo taken

Categories

Resources