Aman Verma Aman Verma - 21 days ago 15
Android Question

How to save SVG in internal Storage And retrieve it and set it to ImageView?

I need to save the .svg in the Internal storage of android application and retrieve it and set it to the ImageView.

I am not able to save the .svg file. i am using this method -

File cacheDir = ctx.getCacheDir();
f = new File(cacheDir, name + ".png");

try {
InputStream in = new java.net.URL(imageurl).openStream();
mIcon = BitmapFactory.decodeStream(in);

try {

FileOutputStream out = new FileOutputStream(
f);
mIcon.compress(
Bitmap.CompressFormat.JPEG,
100, out);
out.flush();
out.close();

return f;

} catch (FileNotFoundException e) {

return null;
} catch (IOException e) {

return null;
}

} catch (Exception e) {

return null;
}

Gut Gut
Answer Source

Why are you decoding SVG into bitmap ? I'm no sure it is possible.

But if you wannna save SVG file in storage, just copy you input stream into output stream.

Simple java solution :

byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
    out.write(buffer, 0, len);
}

or with IOUtils

If you want to display your SVG file, look at https://github.com/pents90/svg-android/tree/master/svgandroid