AFAIK accessing thumbnails for images via MediaStore.Images.Thumbnails would generate thumbnails at first attempt, and that's what I need to perform against specific location on sd card.
The question is how to make valid URI to content under specific folder?
All answers I can find use just MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI as uri to make managedQuery. And result of it is Cursor that points to all sdcard images, while none examples can be found on how to access only specific folder.
Maybe you could just list files in the directory and parse them to get thumbnails without using content provider. You can use inSampleSize option to get small bitmap not the complete image Strange out of memory issue while loading an image to a Bitmap object.
may be is to late, but for some one will be helpfull
Mihai Fonoage said...
Use something like
File imagesDir = new File(Environment.getExternalStorageDirectory().toString() + "/pathToDirectory");
File[] imageList = imagesDir.listFiles();
for (File imagePath : imageList) {
bitmap = BitmapFactory.decodeStream(imagePath.toURL().openStream());}
Here you have some great tutorial.
Related
I am writing an application to store the user profile information and contact information. User profile and contact information also have an image/bitmap. I would like to store images in internal storage probably and display the image using Picasso library. I want to application to create a profile directory to store the profile image and similarly for contacts. I want to use Picasso to retrieve the image from file and display it on the ImvageView as shown below.
Picasso.with(context)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.load("file:///somepath/profile.png")
.into(imageView);
I am not sure how the application will create sub-directories and store the images. What would be the path for images that can be provided to the Picasso to load the image in Imageview?
Edit1
I think, I can use getApplicationContext().getFilesDir().getAbsolutePath() to get the path, but I am still not sure how to create the subdirectories for profile and contacts?
In Android there is a directory in the internal storage where your app can store any kind of file; you can use Context.getFilesDir() to retrieve it.
You can create subdirectories too, using File.mkdir(). I don't know if Picasso takes care of that, but to be sure you can create the directory beforehand. The code to determine the path look like this:
File makeAndGetProfileDirectory(String profileName) {
// determine the profile directory
File profileDirectory = new File(context.getFilesDir(), profileName);
// creates the directory if not present yet
profileDirectory.mkdir();
return profileDirectory;
}
Now you can use this method to obtain a directory where to put the profile data, including the picture. Let's say every profile will have a file named picture.jpg; the code for Picasso would look like this:
String profileName = "foo"; // replace with the profile you want to show
File profileDir = makeAndGetProfileDirectory(profileName);
File profilePictureFile = new File(profileDir, "picture.jpg");
// now use Picasso to read it
Picasso.with(context)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.load(profilePictureFile)
.into(imageView);
Hope this helps.
This path is fine for some devices to fetch path of captured images but this not works for all devices because some does not contain dcim file it self. So i need to fetch path dynamically to all devices is there any way to do this using any api . thanks.
File path = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM
+ "/Camera");
You can query the content resolver using managedQuery. It will return a cursor through which u can access the image/video/sound files.
I have a custom camera that has a public method for getting the thumbnail of that last image saved to a specific folder on the sdcard...
that method looks like this:
public void getGalleryThumb(){
// TODO add Logic for gallery images..
File sdDir = new File("/sdcard/LC/images");
File[] sdDirFiles = sdDir.listFiles();
if(sdDir.length()>0){
File lastPhoto = sdDirFiles[0];
Bitmap myBitmap = BitmapFactory.decodeFile(lastPhoto.getAbsolutePath());
//SET MY IMAGE VIEW BITMAP TO LAST FILE IN sdDIRFiles
photo.setImageBitmap(myBitmap);
btn_gallery.setVisibility(View.VISIBLE);
}
//Toast.makeText(getBaseContext(), "num images in gal:"+sdDirFiles.length +"last image name: "+sdDirFiles[0], Toast.LENGTH_LONG).show();
}
i have noticed that if i delete a photo from that folder the method above does not always retrieve the right image.. I have used:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory()+"/LC/images/")));
upon deleting and writing new files to that sdcard/folder but it doesn't seem to do the trick plus it forces this annoying toast message about the sdcard being mounted..
any help would be appreciated
I was writing this in a comment, but I'll put the detail here. Multiple issues:
listFiles doesn't not guarantee order. You'll need to sort your files by last modified.
You shouldn't access "/sdcard" directly. Your second snippet of code has Environment.getExternalStorageDirectory(). Use that.
You get messages about the SDCARD? Where are they coming from? If you mount it to your local machine, you won't be able to access it from your app while its mounted.
Actually i know how to open PNG files as bitmaps. But my code doens't works for open JPG files, i dont know why.
I can't find correct examples on SO or google about how to do this.
I need to have a bitmap with the JPG file opened from a dir of the sdcard. For example "sdcard/images/01.jpg"
Thanks
File root = Environment.getExternalStorageDirectory();
ImageView IV = (ImageView) findViewById(R.id."image view");
Bitmap bMap = BitmapFactory.decodeFile(root+"/images/01.jpg");
IV.setImageBitmap(bMap);
Always try to use Environment.getExternalStorageDirectory(); instead of sdcard.
You need an ImageView somewhere in your layout, however that's how I do this kind of things.
I use this code personally too, and it works here.
Any of the BitmapFactory.decode* methods should be able to handle standard JPG files.
If you post some code it could be easier to see why it won't work.
I'm attempting to create a gallery/gridview that is loaded with images from a specific folder that resides on an SDCard. The path to the folder is known, ("mnt/sdcard/iWallet/Images") , but in the examples I've seen online I am unsure how or where to specify the path to the pictures folder I want to load images from. I have read through dozens of tutorials, even the HelloGridView tutorial at developer.android.com but those tutorials do not teach me what i am seeking.
Every tutorial I have read so far has either:
A) called the images as a Drawable from the /res folder and put them into an array to be loaded, not using the SDCard at all.
B) Accessed all pictures on the SDCard using the MediaStore but not specifying how to set the path to the folder I want to display images form
or
C) Suggested using BitmapFactory, which I haven't the slightest clue how to use.
If I'm going about this in the wrong way, please let me know and direct me toward the proper method to do what I'm trying to do.
my target android sdk version 1.6...
thanks..
You can directly create Bitmaps from decodeFile (String pathName) that will give you Bitmap object that can be set on ImageView
Update: Below is sudo code with minor errors modify it to suit your needs
File path = new File(Environment.getExternalStorageDirectory(),"iWallet/Images");
if(path.exists())
{
String[] fileNames = path.list();
}
for(int i = 0; i < fileNames .length; i++)
{
Bitmap mBitmap = BitmapFactory.decodeFile(path.getPath()+"/"+ fileNames[i]);
///Now set this bitmap on imageview
}
Actually, you are wrong to mention fixed path to access SD-card directory, because in some device it is /mnt/sdcard and in other /sdcard.
so to access root directory of sd-card, use the getExternalStorageDirectory(), it gives you actual path of root directory.
This function will resturn all the files from specific folder you need to pass path till ur folder
public static List getFilesFromDir(File aStartingDir)
{
List result = new ArrayList();
File[] filesAndDirs = aStartingDir.listFiles();
List filesDirs = Arrays.asList(filesAndDirs);
Iterator filesIter = filesDirs.iterator();
File file = null;
while ( filesIter.hasNext() ) {
file = (File)filesIter.next();
result.add(file); //always add, even if directory
if (!file.isFile()) {
//must be a directory
//recursive call!
List deeperList = getFileListing(file);
result.addAll(deeperList);
}
}
Collections.sort(result);
return result;
}
BitmapDrawable d = new BitmapDrawable(getResources(), path+".jpg"); // path is ur resultant //image
img.setImageDrawable(d);
Hope it help u...
You can access your directory using File java class, then iterate through all the files in there, create a bitmap for each file using Bitmapfactory.decodeFile() then add the bitmaps to your gallery.