Stored File of Bitmap is not displayed when querying for all files in the same folder

[亡魂溺海] 提交于 2019-12-25 05:01:24

问题


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.


回答1:


Ok, once you store file in Device Storage then just trigger the Media Scanner to insert File Entry in MediaStore then your application should be able to get the file path.

To trigger Media Scanner use below code:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

Actually, You have to trigger Media Scanner to get the file entry which you have stored currently. Android device do when its reboot. Or You launch any Native Camera or Gallery Application(If they implemented it).



来源:https://stackoverflow.com/questions/14255010/stored-file-of-bitmap-is-not-displayed-when-querying-for-all-files-in-the-same-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!