0

I am creating GIF based on images in Android. I create the .gif file and save it with FileOutputStream but I don't see it anywhere on sdcard or in gallery app.

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "test.gif");
    FileOutputStream outStream = null;
    try{
        outStream = new FileOutputStream(file);
        outStream.write(generateGIF());
        outStream.close();
        Log.v("Creating gif", "Gif saved");
    }catch(Exception e){
        e.printStackTrace();
    }
    return null;

generateGIF returns byteArray and everything works without error. Did I missed something?

filipp.kowalski
  • 5,495
  • 7
  • 27
  • 45

2 Answers2

1

Did you add these tags in the AndroidManifest.xml?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

These tags tell Android that the app would like to access the SD card. Otherwise, Android blocks the request, making it impossible to save the GIF.

EasyasPi
  • 430
  • 8
  • 8
1

Use MediaScannerConnection to notify the system of new files. That refreshes the Gallery in case of media files and also the MTP/PTP databases when accessing SD card via USB.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • I was thinking about that, but did't know that MediaScannerConnection also influences MTP/PTP connection. I don't see the gif after phone restart, but I will try adding MediaScanner tomorrow. Thanks. – filipp.kowalski Sep 07 '14 at 19:31
  • Yep, that was it. For reading files via Windows Explorer MediaScanner is also needed. – filipp.kowalski Sep 08 '14 at 14:55