Android-将.gif转换为.png的最佳方法 [英] Android - Best way to convert .gif to .png

查看:429
本文介绍了Android-将.gif转换为.png的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器上下载了许多.gif文件,当我尝试在画布上绘制它们时,它永远无法工作.我已经从gifs中创建了位图,并使用Options类将它们设置为不可变的,并尝试仅创建一个新文件并将图像另存为png,但这似乎也不起作用.有人碰巧有一种很好的简单方法将gif转换为png吗?到目前为止,我已经尝试过:

Im downloading lots of .gif files from a server and when I try to draw on them in a canvas it never works. Ive created the bitmaps from the gifs and have set them to be immutable using the Options class and have tried just creating a new file and saving the image as a png but that doesnt seem to work either. Does somebody happen to have a good simple way to convert a gif to a png? So far Ive tried:

Bitmap b = BitmapFactory.decodeFile(mediaStorageDir.getAbsolutePath() + "/" +       
cr.getString(cr.getColumnIndex(gh.Zone_ZoneName))+".gif");
                           OutputStream stream = null;

File f = new File(mediaStorageDir.getAbsolutePath() + "/" + 
cr.getString(cr.getColumnIndex(gh.Zone_ZoneName))+".png");

if(f.createNewFile()){
    System.out.println("PNG CREATED "+f.getAbsolutePath());
}else{
System.out.println("PNG NOT CREATED "+f.getAbsolutePath());
}
stream = new FileOutputStream(f.getAbsolutePath());
b.compress(CompressFormat.PNG, 100, stream);
stream.close();

但这似乎不起作用.我什至都没有看到system.out.println调用,而且我知道代码正在运行,因为正在保存gif上方的代码.

But this doesnt seem to work. I never even see the system.out.println calls either and I know that the code is being run because right above the gif's are being saved.

推荐答案

我发现了我的问题.当我尝试访问该文件以从中创建png时,新创建的文件上存在系统锁定.当我将呼叫移到其他地方以使png正常工作时.

I figured out my problem. There was a system lock on the newly created file when I was trying to access it to make a png from it. When I moved the call elsewhere to make the png it worked fine.

这篇关于Android-将.gif转换为.png的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆