mahdi
mahdi

Reputation: 16407

android : save svg file from web and save on a file

i want to save a svg file from web to a file and then show it from file. i use this code to save a png file :

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();

what should i do for svg file ?

Upvotes: 0

Views: 1045

Answers (1)

Paul LeBeau
Paul LeBeau

Reputation: 101800

In theory, you should be able to do something like the following:

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

However you should not do this. writeToStream() is deprecated (as is createFromStream()). I presume the reason is that the format of a Picture may change in the future and any saved pictures may no longer load. If you are just using it for temporary caching while the app is running, then that may be okay.

But it would be better, as @greenapps says, to cache the original SVGs.

Upvotes: 1

Related Questions