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

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 ?

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.

Related

bitmap.compress destroys the picture quality

I'm using an app to get the gps location and draw it as a circle on a bitmap then save it to proceed, so I need repetitively to read and save the file. But unfortunately when I save the file and read it, the file is damaged after some iterations...! the code:
File output = new File(tmpDirectory, "map.jpg");
try {
OutputStream outputStream = new FileOutputStream(output);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception ex) {
Message("error!");
}
directory = tmpDirectory;//updating directory to load the manipulated image
readFile(directory + "map.jpg", false);//setting the image view new image
image included:picture after iterations
image included:
main image
JPEG uses lossy compression. That means with each iteration you will lose some quality. You should use loseless format like PNG if you want to preserve it.
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);

How to convert PNG image to GIF in Android programatically

I am working an app in which I want to convert images from png format to gif format please help me or some suggestion for that
Use a Gif encoder class to achieve that.
For example, you could use https://github.com/nbadal/android-gif-encoder/blob/master/GifEncoder.java
ByteArrayOutputStream bos = new ByteArrayOutputStream();
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.start(bos);
encoder.addFrame(image);
encoder.finish();
byte[] array = bos.toByteArray();
// Save to file
File output = new File("output.gif");
FileOutputStream fos = new FileOutputStream(output.getPath());
fos.write(array);
fos.close();

Can't get bitmap from drawable (Robolectric)

I am trying to retrieve a bitmap and save to my local disk on PC.
As a result I get a jpeg file (95 bytes) which cannot be read.
source:
Application application = new MyApplication();
Bitmap bitmap = BitmapFactory.decodeResource(application.getResources(), drawableId);
File outputFile = new File("D:\\OutputFile.jpg");
OutputStream os = new FileOutputStream(outputFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
IoUtils.closeSilently(os); //close stream
P.S. I know that I can't get bitmap from drawable. But I don't know why...

How to save image file in BMP format?

the following code compress my image or it is not a BMP file:
FileOutputStream fos = new FileOutputStream(imagefile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
How can I save my image in BMP format?
There's no built-in encoder for BMP according to this reference. BMP not being an overly complex format, it probably wouldn't be rocket science to write/find a Java implementation.
Hey just give the name to .bmp
Do this:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.PNG, 40, bytes);
//you can create a new file name "test.BMP" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "**test.bmp**")
it'll sound that IM JUST FOOLING AROUND but try it once it'll get saved in bmp format..Cheers

Android: how to delete internal image file

What i want to do: delete an image file from the private internal storage in my app. I save images in internal storage so they are deleted on app uninstall.
I have successfully created and saved:
String imageName = System.currentTimeMillis() + ".jpeg";
FileOutputStream fos = openFileOutput(imageName, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 35, fos);
an image that i receive through
bitmap = BitmapFactory.decodeStream(inputStream);
I am able to retrieve the image later for display:
FileInputStream fis = openFileInput(imageName);
ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
DataOutputStream outWriter = new DataOutputStream(bufStream);
int ch;
while((ch = fis.read()) != -1)
outWriter.write(ch);
outWriter.close();
byte[] data = bufStream.toByteArray();
bufStream.close();
fis.close();
imageBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
I now want to delete this file permanently. I have tried creating a new file and deleting it, but the file is not found:
File file = new File(imageName);
file.delete();
I have read on the android developer website that i must open private internal files using the openFileInput(...) method which returns an InputStream allowing me to read the contents, which i don't really care about - i just want to delete it.
can anyone point me in the right direction for deleting a file which is stored in internal storage?
Erg, I found the answer myself. Simple answer too :(
All you have to do is call the deleteFile(imageName) method.
if(activity.deleteFile(imageName))
Log.i(TAG, "Image deleted.");
Done!

Categories

Resources