Reputation: 1071
with the following code I'm trying to save JPEGs to an animated GIF file.
I'm new at developing with Java and Android and do not realy know whats wrong.
Can someone help me to complete this?
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource("/path/to/a/sample/file.mp4");
Bitmap b1 = mmr.getFrameAtTime(0);
Bitmap b2 = mmr.getFrameAtTime(100);
Bitmap b3 = mmr.getFrameAtTime(200);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b1.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray1 = stream.toByteArray();
b2.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray2 = stream.toByteArray();
b3.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray3 = stream.toByteArray();
DataOutputStream writer;
try {
writer = new DataOutputStream(new FileOutputStream("/path/to/a/sample/output/file.gif"));
writer.writeBytes("GIF"); //header
writer.writeBytes("89a");
writer.write(byteArray1, 0, byteArray1.length); //image 1
writer.write(byteArray2, 0, byteArray2.length); //image 2
writer.write(byteArray3, 0, byteArray3.length); //image 3
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 2
Views: 2336
Reputation: 5105
Old question, but I just happened to work on the same stuff. If you have a limited number of frames, you can create a gif (animated pictures I should say) with that:
AnimationDrawable animatedGIF = new AnimationDrawable();
animatedGIF.addFrame(new BitmapDrawable(getResources(), <bitmap1>), <duration>);
animatedGIF.addFrame(new BitmapDrawable(getResources(), <bitmap2>), <duration>);
view.setBackground(animatedGIF); // attach animation to a view
animatedGIF.run();
Upvotes: 2
Reputation: 20059
Whats wrong is that an animated GIF is not a series of normal GIF's. And its not PNG either (why do you compress it as PNG??? well doesn't matter, wont work either way).
Basic introduction of animated GIF format: http://en.wikipedia.org/wiki/Graphics_Interchange_Format#Animated_GIF
As far as I know the standard JRE API's do not support creation of animated GIF's. There are probably 3rd-party libraries that do support it. Expect to write much more code to perform the conversion.
Upvotes: 0