Hi I am trying ot load images stored on the card and figured that its better to load the bitmaps, but I want to have both full path and thumbnail path. How do I do that? I would like my adapter to show me the list of thumbnails but when I click I need to redirect to a preview so I also need a full path. So if my object is Image, I need to have imagePath and imageThumbPath loaded at the same time.
Here is how I load them now:
new Image(data) where data is the path to the actual image. How do I load thumb path simultaneously into same object?
public static List<Image> getCameraImages(Context context) {
final String[] projection = { MediaStore.Images.Media.DATA };
final String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
final String[] selectionArgs = { CAMERA_IMAGE_BUCKET_ID };
final Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
null);
List<Image> result = new ArrayList<Image>(cursor.getCount());
if (cursor.moveToFirst()) {
final int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
do {
final String data = cursor.getString(dataColumn);
result.add(new Image(data));
} while (cursor.moveToNext());
}
cursor.close();
return result;
}
public static final String CAMERA_IMAGE_BUCKET_NAME =
Environment.getExternalStorageDirectory().toString()
+ "/DCIM/Camera";
public static final String CAMERA_IMAGE_BUCKET_ID = getBucketId(CAMERA_IMAGE_BUCKET_NAME);
public static String getBucketId(String path) {
return String.valueOf(path.toLowerCase().hashCode());
}
and I found how to do hat ONLY to find out that the thumbs are all same size as originals!!! Horrible!
Related
It is possible to recognize whether the file obtained using "Media file access" is stored on the SD card or in the Internal Memory ? For example, is it possible to find out the information from a media URI? In my opinion, it is only possible to find out the name of a subdirectory, but then it is no longer possible to find out the name of parent subdirectory.
Thanks, Michal
class Video {
private final Uri uri;
private final String name;
private final int duration;
private final int size;
public Video(Uri uri, String name, int duration, int size) {
this.uri = uri;
this.name = name;
this.duration = duration;
this.size = size;
}
}
List<Video> videoList = new ArrayList<Video>();
String[] projection = new String[] {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.SIZE
};
String selection = MediaStore.Video.Media.DURATION +
" >= ?";
String[] selectionArgs = new String[] {
String.valueOf(TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES));
};
String sortOrder = MediaStore.Video.Media.DISPLAY_NAME + " ASC";
try (Cursor cursor = getApplicationContext().getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder
)) {
// Cache column indices.
int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
int nameColumn =
cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
int durationColumn =
cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
int sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
while (cursor.moveToNext()) {
// Get values of columns for a given video.
long id = cursor.getLong(idColumn);
String name = cursor.getString(nameColumn);
int duration = cursor.getInt(durationColumn);
int size = cursor.getInt(sizeColumn);
Uri contentUri = ContentUris.withAppendedId(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id);
// Stores column values and the contentUri in a local object
// that represents the media file.
videoList.add(new Video(contentUri, name, duration, size));
}
}
it is possible to somehow find out that the video is on the SD card or in the memory.
I have a simple question.
It's possible to access to DCIM folder (internal storage) from an app and retrieve an image?
I've found solutions that use something like Enviroment.getExternalStoragePublicDirectory but I don't understand why.
Environment.getExternalStorageDirectory().toString()+ "/DCIM/Camera";
or
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()
Will give you a path for the public directory. This path is required to fetch all files including image. getExternalStorageDirectory() and Environment.getExternalStoragePublicDirectory() is public so you can access them from any installed app.
The following code snippet can be used to list all image file path from "DCIM" folder:
public static List<String> getCameraImages(Context context) {
public final String CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().toString()+ "/DCIM/Camera";
public final String CAMERA_IMAGE_BUCKET_ID = String.valueOf(CAMERA_IMAGE_BUCKET_NAME.toLowerCase().hashCode());
final String[] projection = { MediaStore.Images.Media.DATA };
final String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
final String[] selectionArgs = { CAMERA_IMAGE_BUCKET_ID };
final Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
null);
ArrayList<String> result = new ArrayList<String>(cursor.getCount());
if (cursor.moveToFirst()) {
final int dataColumn =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
do {
final String data = cursor.getString(dataColumn);
result.add(data);
} while (cursor.moveToNext());
}
cursor.close();
return result;
}
I have used following code to get list of directories
String ROOT_DIR = Environment.getExternalStorageDirectory().getPath();
ROOT_DIR is passed is as string
public static ArrayList<String> getDirectoryPaths(String directory) {
ArrayList<String> pathArray = new ArrayList<>();
File file = new File(directory);
File[] listfiles = file.listFiles();
if (listfiles != null) {
for (int i = 0; i < listfiles.length; i++) {
if (listfiles[i].isDirectory()) {
pathArray.add(listfiles[i].getAbsolutePath());
}
}
}
return pathArray;
}
`
It returns all directories path but i want to get the path of the directories containing images only like it shows in gallery Eg (Camera, WhatsAppImages, hike,...); I want to get path of all of them.
Any ideas?
Try this:
Create a Bucket Class
public class Bucket {
private String name;
private String firstImageContainedPath;
public Bucket(String name, String firstImageContainedPath) {
this.name = name;
this.firstImageContainedPath = firstImageContainedPath;
}
public String getName() {
return name;
}
public String getFirstImageContainedPath() {
return firstImageContainedPath;
}
}
Then, Add this method, it is going to return all the buckets where there are Images.
public static List<Bucket> getImageBuckets(Context mContext){
List<Bucket> buckets = new ArrayList<>();
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String [] projection = {MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.DATA};
Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null, null);
if(cursor != null){
File file;
while (cursor.moveToNext()){
String bucketPath = cursor.getString(cursor.getColumnIndex(projection[0]));
String fisrtImage = cursor.getString(cursor.getColumnIndex(projection[1]));
file = new File(fisrtImage);
if (file.exists() && !bucketSet.contains(bucketName)) {
buckets.add(new Bucket(bucketName, fisrtImage));
}
}
cursor.close();
}
return buckets;
}
Finally, create your custom spinner item and fill your spinner with an adapter.
Next Step is to fill the gridview with images from the selected bucket.
This method is going to return all the images according the bucketpath.
public List<String> getImagesByBucket(#NonNull String bucketPath){
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String [] projection = {MediaStore.Images.Media.DATA};
String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME+" =?";
String orderBy = MediaStore.Images.Media.DATE_ADDED+" DESC";
List<String> images = new ArrayList<>();
Cursor cursor = mContext.getContentResolver().query(uri, projection, selection,new String[]{bucketPath}, orderBy);
if(cursor != null){
File file;
while (cursor.moveToNext()){
String path = cursor.getString(cursor.getColumnIndex(projection[0]));
file = new File(path);
if (file.exists() && !images.contains(path)) {
images.add(path);
}
}
cursor.close();
}
return images;
}
Finally, create your adapter and fill your gridview.
I hope, It can help you.
How do you get a list of all camera images of an Android device?
Is it through the MediaStore? How?
The Gallery app obtains camera images by using a content resolver over Images.Media.EXTERNAL_CONTENT_URI and filtering the results by Media.BUCKET_ID. The bucket identifier is determined with the following code:
public static final String CAMERA_IMAGE_BUCKET_NAME =
Environment.getExternalStorageDirectory().toString()
+ "/DCIM/Camera";
public static final String CAMERA_IMAGE_BUCKET_ID =
getBucketId(CAMERA_IMAGE_BUCKET_NAME);
/**
* Matches code in MediaProvider.computeBucketValues. Should be a common
* function.
*/
public static String getBucketId(String path) {
return String.valueOf(path.toLowerCase().hashCode());
}
Based on that, here's a snippet to get all camera images:
public static List<String> getCameraImages(Context context) {
final String[] projection = { MediaStore.Images.Media.DATA };
final String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
final String[] selectionArgs = { CAMERA_IMAGE_BUCKET_ID };
final Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
null);
ArrayList<String> result = new ArrayList<String>(cursor.getCount());
if (cursor.moveToFirst()) {
final int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
do {
final String data = cursor.getString(dataColumn);
result.add(data);
} while (cursor.moveToNext());
}
cursor.close();
return result;
}
For more info, review the ImageManager and ImageList classes of the Gallery app source code.
I am looking for a way to find images on the SD card, or at least images taken by the camera.
The ideal solution would be getting a collection of file paths or URIs for the images. I guessing you can do this through the MediaStore I just can't figure out how.
Thanks
After looking at more MediaStore examples I came up with this and it gets the job done.
protected ArrayList<Uri> GetImageList(boolean getThumbs)
{
ArrayList<Uri> images = new ArrayList<Uri>();
String columnType;
Uri contentType;
String[] columnNames;
if (getThumbs)
{
columnType = MediaStore.Images.Thumbnails._ID;
contentType = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
}
else
{
columnType = MediaStore.Images.Media._ID;
contentType = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
columnNames = new String[]{columnType};
Cursor cursor = managedQuery(contentType, columnNames, null, null, null);
int columnIndex = cursor.getColumnIndexOrThrow(columnType);
for(int i = 0; i < cursor.getCount(); i++)
{
cursor.moveToPosition(i);
int id = cursor.getInt(columnIndex);
images.add(Uri.withAppendedPath(contentType, "" + id));
}
return images;
}
It returns the Uri of all the images or the Uri of the thumbnail of all the images on the sdcard.