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...
Related
I make canvas view which I want to send in server using retrofit library. I get canvas view in bitmap format. Now I want to convert bitmap to file format so that I can upload in server in file format.
File file = new File(context.getCacheDir(), filename);
file.createNewFile();
/* Convert bitmap to byte array */
Bitmap bitmap = bitmap; //bitmap is your bitmap file which you want to convert
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 , bos);
byte[] bitmapdata = bos.toByteArray();
/* write the bytes in file */
FileOutputStream fos = new FileOutputStream(file);
fos.write(bitmapdata);
fos.flush();
fos.close();
Always close and flush the FileOutputStream.
Convert ua bitmap to base64 String like below and send it using string
public static String bitmapToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)//u can pass 100 in quality or any integer
{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}
my bitmap image in the bitmap variable whose name is imageFile how we further give name this bitmap image and store it in my mobile memory
in this picture a function show which receive image file and convert it into bitmap
1- you have to find the path to where you wanna save your image.
2- open an OutputStream object and give it the file path.
3- save your bitmap to the output stream.
4- flush.
5- close.
// Get environment dir
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "MyImage"+".jpg"); // the File to save to
fOut = new FileOutputStream(file);
Bitmap pictureBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), optons); // obtaining the Bitmap
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush(); // Not really required
fOut.close(); // do not forget to close the stream
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);
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.
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