问题
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();
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.
回答1:
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.
来源:https://stackoverflow.com/questions/17310745/android-best-way-to-convert-gif-to-png