android:从web保存svg文件并保存在文件中

时间:2015-02-01 17:37:13

标签: android file svg bitmap

我想将svg文件从web保存到文件,然后从文件中显示。我使用此代码保存png文件:

OutputStream fos = null;
File file = new File(getApplicationContext().getCacheDir(),FilenameUtils.getBaseName(url.toString())+FilenameUtils.getExtension(url.toString()));
Bitmap bm = ((BitmapDrawable) drawable).getBitmap();
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bm.compress(Bitmap.CompressFormat.PNG, 50, bos);
bos.flush();
bos.close();

我该怎么做svg文件?

1 个答案:

答案 0 :(得分:1)

理论上,您应该能够执行以下操作:

PictureDrawable pd = (PictureDrawable) imageView.getPicture();
Picture picture = pd.getPicture();
picture.writeToStream(os);

但是你不应该这样做。 writeToStream()已被弃用(createFromStream()也是如此)。我认为原因是图片的格式将来可能会改变,任何保存的图片可能不再加载。如果您只是在应用程序运行时将其用于临时缓存,那么可能可以。

但是,正如@greenapps所说,它会更好地缓存原始的SVG。