android convert bitmap to bufferedinputstream - android

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.

Related

How to convert bitmap image to file format which I get from canvas in android

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

How to upload Image in localhost database in the form of string from android? Not Path only image

I want to upload my gallery image to localhost database in the form of string from android but i did not found any relevant topic. Please guide me
you can convert you Image bitmap to base64 String by this code :`
`ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

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

Android - converting File or Byte array into a Blob

I've been searching a lot but i couldn't find an answer for this simple question.
I would like to implement one of the following functions:
public Blob getBlob(Byte[] imageByteArray){
}
public Blob getBlob(File imageFile){
}
please note that these functions are being called from the android client.
thanks!
//you bitmap image first get
Bitmap bitmap = BitmapFactory.decodeFile("/path/images/image.jpg");
//take on bytearrayoutputStream to convert into blolb so here is you blob
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 "ignore png", blob);
byte[] bitmapdata = blob.toByteArray();

Bitmap to String using Base64. How can fix it?

The Base64.encode doesn't want to take the argument "image", and I don't know how to figure out. I've never used Base64 before.
Bitmap bm = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] image = baos.toByteArray();
String encodedImage = Base64.encode(image);
Edit: I use an external package of Base64 http://iharder.sourceforge.net/current/java/base64/
Base64 encode takes at least two arguments. Perhaps try Base64.encode(image, Base64.DEFAULT)

Categories

Resources