Get images to Gallery instantly

Hey guys,

I'm working on a tiny app, which creates images. Most parts are working pretty well, some bugs as usually.
But one thing is making me mad. If I create an Image (JPG) it needs at least 10 Minutes, sometimes a reboot, till the images appears in the gallery.
Did anyone got that problem, and if, how did you solved it?

Here is my code
class savePic { public void Save(string directory, Bitmap bitmap) { int dirLength = directory.Length; for (int i = dirLength; i > 0; i--) { if (directory.Substring((i - 1), 1) == ".") { directory = directory.Substring(0, i - 1) + "_overlay.jpg"; } } var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.Path; var filePath = System.IO.Path.Combine(sdCardPath, directory); var stream = new FileStream(filePath, FileMode.Create); bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream); stream.Close(); } }

Regards
Buzz

Posts

  • D3nnisD3nnis USUniversity ✭✭✭

    Try this one to update the image in the gallery:

    SendBroadcast (new Intent (Intent.ActionMediaScannerScanFile, Uri.Parse("file://" + Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "file location")));

    In your case it would prabably be something like this:

    SendBroadcast (new Intent (Intent.ActionMediaScannerScanFile, Uri.Parse(filePath)));

  • BuzzTycaBuzzTyca DEMember

    @D3nnis
    Thanks for your Answer.
    I tried your code, but it didn't work, so I asked google about it, and found, that the code doesn't work anymore since Android 4.4. cause of sercurity permissions.
    Tell me, if I'm wrong.

    Now I'm looking for an other way.
    But you kicked me in the right direction.
    Thanks for that.

  • D3nnisD3nnis USUniversity ✭✭✭

    The depricated version is with:
    Intent.ActionMediaMounted

    this version uses
    Intent.ActionMediaScannerScanFile

    The depricated version crashes the app, the new version works or it doesn't do anything at all.

    You need to make sure that the your Uri points to the right location including the name of the file. I had this working yesterday.
    One little typo and it doesn't work. The best thing you could do is use the same Uri for saving the file as for running the Broadcast.

    I used this Uri for the file that I was using:
    Uri.Parse("file://" + Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/FolderName/ImageName.jpg")

    Which will be this line of code in total:

    SendBroadcast (new Intent (Intent.ActionMediaScannerScanFile, Uri.Parse("file://" + Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/FolderName/ImageName.jpg")));

  • BuzzTycaBuzzTyca DEMember
    edited January 2017

    @D3nnis
    Thanks so much!
    It finally works well.
    My locationString was incorrect, so Android couldn't find the image.

    Regards Buzz

  • D3nnisD3nnis USUniversity ✭✭✭

    @BuzzTyca Great to hear that it works! It also took myself some trial and error to make it work.
    Can you select the post as the answer so that it might help people in the future?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @D3nnis said:
    The depricated version is with:
    Intent.ActionMediaMounted

    this version uses
    Intent.ActionMediaScannerScanFile

    The depricated version crashes the app, the new version works or it doesn't do anything at all.

    You need to make sure that the your Uri points to the right location including the name of the file. I had this working yesterday.
    One little typo and it doesn't work. The best thing you could do is use the same Uri for saving the file as for running the Broadcast.

    I used this Uri for the file that I was using:
    Uri.Parse("file://" + Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/FolderName/ImageName.jpg")

    Which will be this line of code in total:

    SendBroadcast (new Intent (Intent.ActionMediaScannerScanFile, Uri.Parse("file://" + Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/FolderName/ImageName.jpg")));

    It seems that Uri.Parse does not exists...

  • D3nnisD3nnis USUniversity ✭✭✭

    @AlessandroCaliaro said:
    It seems that Uri.Parse does not exists...

    You are probably refering to the wrong Uri class. Try: Android.Net.Uri.Parse().

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @D3nnis said:

    @AlessandroCaliaro said:
    It seems that Uri.Parse does not exists...

    You are probably refering to the wrong Uri class. Try: Android.Net.Uri.Parse().

    you are right

Sign In or Register to comment.