The below code returns(test.gif) only the first frame and makes a gif with the 5 same frames (video first frame).
I read most questions in StackOverflow but I can't figure out what cause this problem.
Also, per some suggestions, I tried to use mmRetriever.getFrameAtTime
with MediaMetadataRetriever.OPTION_CLOSEST
option with no success.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Uri vid = data.getData();
videoPath = getPath(vid);
MediaMetadataRetriever mmRetriever = new MediaMetadataRetriever();
mmRetriever.setDataSource(videoPath);
String METADATA_KEY_DURATION = mmRetriever
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.setDelay(30);
encoder.start(bos);
int max = (int) Long.parseLong(METADATA_KEY_DURATION);
Log.i("max",String.valueOf(max));
for (int i=1000000;i<max*1000;i+=1000000) {
Log.i("i",String.valueOf(i));
Bitmap bitmap = mmRetriever.getFrameAtTime(i,MediaMetadataRetriever.OPTION_CLOSEST);
encoder.addFrame(bitmap);
}
encoder.finish();
try {
String filepath = Environment.getExternalStorageDirectory()
+ File.separator + "test.gif";
FileOutputStream outStream = new FileOutputStream(filepath);
outStream.write(bos.toByteArray());
outStream.close();
GifAnimationDrawable big = new GifAnimationDrawable(
new FileInputStream(filepath));
imageview.setImageDrawable(big);
big.setVisible(true, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Log return max value about 5000