How to convert PNG image to GIF in Android programatically - android

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

Related

Compare size of file and size of byte array of that file

When we convert the file (say - image)
To byteArray how can be approximately compared between them (image file and byteArray) ?
Is there also a way to first convert image file to byteArray and this byteArray has to be written in a text-file..
Again after this read these lines of byteArray from text-file and make byteArray and hence converted to image file...... I'm beginner and just for knowledge purpose i want to know....
You can convert your image to byte array like this.
BufferedImage bImage = ImageIO.read(new File("sample.jpg"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", bos );
byte [] data = bos.toByteArray();
Yes you can directly compare 2 byteArray.
Just do something like
if (bytearray1.length == bytearray2.length)
//its same
Try this to write the ByteArray in text file
public void writeToFile(byte[] array)
{
try
{
String path = "/data/data/YOURFILE.txt"; //provide your path here
File file = new File(path);
if (!file.exists()) { //just to check if file is actually present
file.createNewFile();
}
FileOutputStream stream = new FileOutputStream(path);
stream.write(array);
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
}

How to get image path of an image stored using Context.MODE_PRIVATE

Hi I successfully stored and get image using following code
To store image
FileOutputStream fos = getActivity().openFileOutput(picName, Context.MODE_PRIVATE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
compressed_bitmap.compress(Bitmap.CompressFormat.JPEG, 95, bos);
byte[] img = bos.toByteArray();
fos.write(img);
fos.close();
To get image
InputStream input = getActivity().openFileInput(imageName);
Bitmap bitmap = BitmapFactory.decodeStream(input,null,null);
but also i need to know the path.How to get the path where the image is stored.Any help will be appreciated and thanks in advance.
You can try this method in your fragment
String absolutePath = getActivity().getFileStreamPath(imageName);

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 ?
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.

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 convert bitmap to bufferedinputstream

Newb question;
I've got a bitmap in memory;
Private Bitmap MyPicture;
Then later, I fill that MyPicture from imager from the camera. I need to upload that photo using the FTP client from the apache commons.
fcon.storeFile("filename", new BufferedInputStream(MyPicture.????));
But the apache want a BufferedInputStream. How do I convert the memory Bitmap to a memory stream?
Thanks guys!
This is what I was looking for;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
si.Image.compress(CompressFormat.JPEG, 100, stream);
InputStream is = new ByteArrayInputStream(stream.toByteArray());
The last line was the missing link...
Converting the bitmap to a byte array is as easy as:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MyPicture.compress(Bitmap.CompressFormat.PNG, 100, baos);
baos.toByteArray();
And then you can create a BufferedInputStreamto write the bytes to your file.

Categories

Resources