Send image to server through httpmultipart - android

I am making an android app in which i am sending images from gallery to server through xml..Any type of help will be appreciated... hanks

This is how I handle it in my application:
// bitmap is your Bitmap object
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// The next line should be adjust to use the format and compression you want.
bitmap.compress(CompressFormat.PNG, 0, stream);
byte[] byteArr = stream.toByteArray();
// The next line would be where you write the byte array to your xml structure:
myXml += Base64.encodeBase64String( byteArr );
In my application, the byte[] gets saved to the db as a blob prior to the xml structure being created. So this code isn't exactly what I'm doing. But it should give you the idea.

Related

Create Database with ready Data

I want store my Images in Database. I know How create this database, but How I can insert Images in it?
I wanted save them in Resources and then add in database, but then I can;t delete them.
I want hold Images in android app, insert them in database and delete from holded place. How I can do it.
Thanks.
You can do this by converting your 'image bitmap' to 'byte array string',
Bitmap bitmap = YOUR_BITMAP;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
byte[] byteArray = outStream.toByteArray();
String imageString=new String(byteArray);
You can save this imageString to DB.
While retrieving you can do the following,
byte[] byteArray = imageString.getBytes();
Bitmap bitmap = BitmapFactory.decodeByteArray( byteArray, 0, byteArray.length);
I hope it will help you.

Storing Image in VCard and reading from it in android code manually

I am creating vCard files from my android app.
I am storing data manually(without using any libray)
I am able to write the data,read and parse it in my app.But when I save Image .I have gor two issues.
1)I am not able to save the image captured using camera..which throws an Out of memoery exception while writing the base 64 encoded string into vcard.
2)I am able to save the base 64 encoded string of some image which I took from gallery,but while reading it doesn't get me image.I am reding all the data from vCard line by line and base64 encoded string is not coming as a single line.(Please note that I stored each value to the file using \r\n)
Please let me know the proper way of doing this.
Code Snippets
Encoding
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
encodedProfileImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
Writing
fw = new FileWriter(vcfFile);
...
fw.write("PHOTO;ENCODING=BASE64;TYPE=PNG:"+encodedProfileImage + "\r\n");
Reading and decoding
else if(strline[0].equals("PHOTO;ENCODING=BASE64;TYPE=PNG")){
String imagestr=strline[1];
byte[] byteArray = Base64.decode(imagestr, Base64.DEFAULT);
card.profileImage = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
}
When reading it in you can use BitmapFactory.Options
http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
Use the decodeByteArray call that takes BitmapFactory.Options as the last argument.
if you set inSampleSize = 2
It will reduce the size of the incoming image to something managable.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; //Scale it down
options.inPreferredConfig = Bitmap.Config.RBG_565; // Less memory

Image Converted to Byte not shown when Converting back to Image

In my android application , i want to convert image into bytes and encode it. and send to database. But when i convert it back on image, it do not show. Please help.and tell me where i am making mistake
final Bitmap image=(images.get(position));
int size = image.getRowBytes() * image.getHeight();
ByteBuffer buffer = ByteBuffer.allocate(size); //Create a new buffer
image.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
byte[] array = buffer.array();
encodedImageString = Base64.encodeToString(array, Base64.DEFAULT);
Now on the server side when i decode this encoded imageString and write it, it do not display image.
Byte[] imageByteArary= base64.decode(encodedImageString);
File myfile=new File("D://test1.jpg");
myfile.createNewFile();
FileOutputStream fOut=new FileOutputStream (myfile);
fOut.write(imageByteArray);
fOut.close();
I was facing the same problem.The String that you send from client side is not same what you receive at server side.
Check this for solution

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

Save image in SQLite database getting from the webservice

I'm working on android app in which i parse the data from the webservice and then store it in the database. here i also want to store the image in the database and retrive back in the next activity.
I'm using this code for inserting the image in database.
Bitmap yourSelectedImage;
ByteArrayOutputStream stream;
byte[] byteArray;
this is in the for loop
{
yourSelectedImage = BitmapFactory.decodeFile(SingleImageURL[i]);
stream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100,
stream);
byteArray = stream.toByteArray();
database.insertDetail(ID[i],byteArray[i]);
}
Here SingleImageURL have the image url that is coming from the webservice.
when i'm run the code it will give error nullPointerException in this line
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100,
stream);
please tell me where i'm going wrong. and how to do this.
Load your Bitmap yourSelectedImage using this link. and then save it to database by converting it to byte[]
You are decoding a file, which actually is a URL. So ur yourSelectedImage is null. And when you try to compress it using
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
you get NullPointerException

Categories

Resources