not always getting the right image - android

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.

Related

Deleted picture still visible in gallery

I have a small problem here.
In my application I let the user select a picture from the gallery. I save the path to it before doing anything else.
When the user picks the picture he wants, I want it to be copied in a other folder, and then deleted from the original one.
Well, it kiiinda works. The original picture is deleted, and a copy appears in the other folder.
Buuut. It's still there. The deleted picture can still be seen in the gallery, and the copy can't be seen. When I call Gdx.files.absolute(originalPath).exists() it returns false, and Gdx.files.external(copyPath).exists() it returns true, and I can work with the copy of the picture with no problem.
It looks like the gallery is not updated.
I use this to delete and copy a picture :
public void MoveToCustomFolder() {
if (DoesOriginalPathExist()) {
if (!DoesCopyExist()) {
System.out.println("Copying");
Gdx.files.external("/CustomFolder/" + fileName).write(Gdx.files.absolute(filePath).read(), true);
}
System.out.println("Deleting");
Gdx.files.absolute(filePath).delete();
}
}
filePath being the absolutePath of the original picture in the gallery and fileName the name of the file ("picture.jpg")
I found something during my research. When clear the data of the media storage application, after little time the correct gallery shows up, with no deleted pictures and with copies where they belong.
Also, I do have the WRITE_EXTERNAL_STORAGE permission.
Do you guys know what's wrong ?
Found the solution.
I had to update the Gallery with this function :
public void UpdateGallery(String filePath) {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(filePath))));
}

Android KitKat MediaScannerConnection not finding images saved to private app folder

I have an app which performs processing on an image selected by the user using from the gallery, after processing the modified image is then saved back to the original image's folder with a modified filename. On specific devices (mainly Samsungs) the new KitKat permission limitations severely limit where a non system app can write to e.g. external SD card or back to typical gallery locations.
The documentation advises to write to the private package specific folder obtained via
getExternalFilesDir(null)
This is fine and the image is saved correctly but then a subsequent call to
MediaScannerConnection.scanFile
fails to update the central media database correctly as the modified image doesn't appear in the gallery, even a reboot of the device which should force a full scan of all media doesn't trigger the image to be displayed.
In other words the following code doesn't work:
File appDir = MyActivity.this.getExternalFilesDir(null);
File file = new File(appDir, "new_image.jpeg");
// Process image and write file away...
MediaScannerConnection.scanFile(this, new String[] { file.getAbsolutePath() }, null, new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Log.i(LOG_TAG, String.format("Scanned file: %s", path));
}
});
The onScanCompleted method fires but the gallery doesn't show the new image.
Has anyone seen this behaviour, does MediaScanner not scan 'private' app folders and if so how else do I get the gallery to detect and display the new image.
If you want images to be picked up by the gallery, you should put them in the proper public location. You can get the proper location using Environment.getExternalStoragePublicDirectory().

load Image from specific folder on the sdcard?

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.

Android picture saved to SD card not showing in Gallery

Currently I have a program that takes pictures and saves them as a jpg on the top level of the sdcard, but its not appearing in the Gallery. Is there something I must do to make it happen?
You need to call the MediaScanner so that it knows your file exists:
File file; // = your file
String mimetype; // = your file's mimetype. MimeTypeMap may be useful.
MediaScanner.scanFile(getApplicationContext(), new String[]{file.getPath()}, new String[]{mimetype}, null);
Try this answer. Working for me
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(finalFile)));
Maybe someone is "overseeing" it...
Take a look at:
https://stackoverflow.com/a/5110571/371749

Android: getting thumbnails from specific location on sd card

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.

Categories

Resources