I am storing a Bitmap in the SD card of the mobile, then I have another method that lists all File present in the folder where I saved my Bitmap. The problem is that when the images are stored, and even though I checked whether they are really present in the folder, my codes to list images from that folder doesn't return those images, it usually shows them after a few times I utilise the app though. Can anyone help me with this? You can find my codes below.
This is for storing the Bitmap
imagesFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "ILoveKitties");
if(!imagesFolder.exists())
imagesFolder.mkdirs();
int imageNum;
if(imagesFolder.list()==null)
imageNum = 1;
else
imageNum = imagesFolder.list().length + 1;
String fileName = "appname_" + String.valueOf(imageNum) + ".jpg";
output = new File(imagesFolder, fileName);
while(output.exists()){
imageNum++;
fileName = "appname_" + String.valueOf(imageNum) + ".jpg";
output = new File(imagesFolder, fileName);
}
OutputStream fOut = new FileOutputStream(output);
merged.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
fOut = null;
saved = true;
savedFilePath = "file://" + output.getCanonicalPath();
And this is for retrieving a list of files in the folder
boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent) {
String[] projection = {MediaStore.Images.Media.DATA};
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%APPNAME%"},
null);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
File testFileAvailability;
for(int i=cursor.getCount()-1; i>=0; i--){
cursor.moveToPosition(i);
// get image filename
testFileAvailability = new File("file://" + cursor.getString(columnIndex));
if(testFileAvailability.exists()){
listImagePath.add("file://" + cursor.getString(columnIndex));
}
}
}
I then use the path to display the images.