How to save image file in BMP format? - android

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

Related

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

bitmap to file that can be uploaded via async

After a day of headaches due to previous problems implementing a working photo upload system I feel im in the home stretch. my last and final step is to allow my users to upload a image once it has been cropped.
after cropping takes place I have access to a bitmap and a imageView that is using the bitmap.The async request lib im using is : http://loopj.com/android-async-http/
and the api im using is setup in such a way that i need to send over a "file" like so:
File myFile = new File("/path/to/file.png");
RequestParams params = new RequestParams();
try {
params.put("profile_picture", myFile);
} catch(FileNotFoundException e) {}
What are my options for turning my bitmap into a "file"
You can save Bitmap to file using Bitmap.compress method. Just provide proper FileOutputStream as argument.
You can also upload image without using files, just save it to byte array and then upload as that array.
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 85, out);
byte[] myByteArray = out.toByteArray();
RequestParams params = new RequestParams();
params.put("profile_picture", new ByteArrayInputStream(myByteArray), "image.png");
Try this:
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/to";
File dir = new File(file_path);
if(!dir.exists)
dir.mkdirs();
File file = new File(dir, "file.png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
and put the below permission in your manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

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

Categories

Resources