2

I'm querying all images on the Android device as such:

string[] columns = { MediaStore.Images.Media.InterfaceConsts.Data, 
            MediaStore.Images.Media.InterfaceConsts.Id };
string orderBy = MediaStore.Images.Media.InterfaceConsts.Id;
var imagecursor = ManagedQuery(MediaStore.Images.Media.ExternalContentUri, columns, null, null, orderBy);
for (int i = 0; i < this.Count; i++) {
    imagecursor.MoveToPosition(i);
    Paths[i]= imagecursor.GetString(dataColumnIndex);
    Console.WriteLine(Paths[i]);
    Console.WriteLine(System.IO.File.Exists(Paths[i]));
}

The problem is that the output shows that some files don't exist. Here's a sample output:

/storage/sdcard0/Download/On-Yom-Kippur-Jews-choose-different-shoes-VSETQJ6-x-large.jpg
False
/storage/sdcard0/Download/397277_10151250943161341_876027377_n.jpg
False
/storage/sdcard0/Download/Roxy_Cottontail_&_Melo-X_Present..._Some_Bunny_Love's_You.jpg
False
/storage/sdcard0/Download/album-The-Rolling-Stones-Some-Girls.jpg
True
/storage/sdcard0/Download/some-people-ust-dont-appreciate-fashion[1].jpg
True
/storage/sdcard0/Download/express.gif
True
...
/storage/sdcard0/Download/some-joys-are-expressed-better-in-silence.JPG
False

How is this possible? I downloaded these images myself from the internet! They should exist in disk.

Jonas Stawski
  • 6,682
  • 6
  • 61
  • 106
  • 1
    What API level are you compiling with and do you have the correct EXTERNAL_STORAGE permissions? – Morrison Chang Oct 11 '12 at 21:48
  • I am targeting framework 2.3. I didn't have the READ_EXTERNAL_STORAGE, but I did have the WRITE_EXTERNAL_STORAGE. Adding the READ one yields the same results – Jonas Stawski Oct 11 '12 at 21:55
  • I am using the Android Support V4 – Jonas Stawski Oct 11 '12 at 22:00
  • Have you scanned your downloads directory with MediaScannerConnection? – Morrison Chang Oct 11 '12 at 22:07
  • I think the problem was that those images were corrupted. I went into the gallery and saw that the affected images did not have a width and height set. Furthermore, some of the images were not displaying on full screen mode. I deleted those images and downloaded more and now File.Exists return true for all images. – Jonas Stawski Oct 11 '12 at 22:10
  • I was wrong. The problem is now happening again! What could be the problem? And also, I have since changed the code to use the LoaderManager as ManagedQuery is deprecated. – Jonas Stawski Oct 12 '12 at 18:50
  • @MorrisonChang no I have not. What do I gain from using the MediaScannerConnection? – Jonas Stawski Oct 12 '12 at 18:57
  • I might be making assumptions so perhaps you can flesh out your question further by describing what you want to accomplish and what you think your code is doing. – Morrison Chang Oct 12 '12 at 19:23
  • I basically wrote my own gallery. I'm displaying all the images in a listview so we can implement multi select of images. – Jonas Stawski Oct 12 '12 at 19:38

2 Answers2

0

You appear to be using a LoaderMananger/ManagedQuery to query the Media Content Provider in Android. A Content Provider is just a way to access a particular SQLite database from different apps. If you use the Android provided Media Content Provider you'll have to update it 'manually' by using MediaScannerConnection to add in the new files that you've placed, as the 'service' may or may not update internally while your app is running.

Here are some related SO questions: Scan Android SD card for new files and Trigger mediascanner on specific path (folder), how to? but I don't recommend the answer of globally scanning your SD card.

Community
  • 1
  • 1
Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
0

If this is a Samsung device, all those paths may be incorrect !

// Put this in your code and then log the 'external' string to see

String external = Environment.getExternalStorageDirectory();

if you get say: /storage/sdcard0/

Then Samsung themselves say you have to do this lame append (I claim they broke the API :)

external = external + "/external_sd/";

I am not certain this applies to downloads or not, but I suspect it affects ALL similar sub-paths.

http://developer.samsung.com/forum/board/thread/view.do?boardName=GeneralB&messageId=162934&messageNumber=1381&startId=zzzzz~&searchType=TITLE&searchText=sdcard

[EDIT:] Caveat Emptor ! Even this does not work on some Samsung devices. ARRRRGGGGGG

Howard Pautz
  • 415
  • 7
  • 21