user8887422
user8887422

Reputation:

How to save a .GIF file into the gallery?

Why is this so difficult to do in android? I know its easy for images, but why not .gifs?

I have this code here which saves it to an SD card, but I am trying to account for the user not having an SD card.

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/dir1/dir2");
dir.mkdirs();
File file = new File(dir, "GIFName_" + System.currentTimeMillis() +".gif");

try {
    FileOutputStream f = new FileOutputStream(file);
    f.write(generateGIF(list));

} catch(Exception e) {
    e.printStackTrace();
}

My app basically converts images to .GIFS, and right now it saves it on the sd card, but I want to save it to the gallery. Is there any way to do this easily? I know you can do it for images, but can you for .GIFS that are created?

Upvotes: 0

Views: 637

Answers (1)

Henry
Henry

Reputation: 17841

What exactly do you mean by Gallery? There is no directory called Gallery. However there is an application called Gallery and I hope that's what you mean.

Environment.getExternalStorageDirectory() will return the root path to the external storage. This has no dependency to the file you are trying to save. If you want save to the Pictures directory, then you can do Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)

Gallery is an application in android that scans the whole system and adds all media items to it. If you try saving a file, be it .gif or .jpg, or .png, programmatically in Android, there is no guarantee that the file will be picked up by Gallery immediately. That's why you need to use MediaScannerConnection. This will let you add your newly created file to be shown in Gallery.

Something like below:

MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null, null);

Documentation: https://developer.android.com/reference/android/media/MediaScannerConnection.html#scanFile(java.lang.String, java.lang.String)

Upvotes: 1

Related Questions