I want to add a image that i take from phones camera
and add it to a html that is build dynamically from user input.
so i cant get the html file from assets.
all the question/answers i found are for local images and html from assets.
Android - local image in webview
Android Development: Using Image From Assets In A WebView's HTML
any suggestions?
Thanks
One of the answers in the links you showed, suggests to use Base64 embedded image:
(https://stackoverflow.com/a/9000691/1271435)
<img src="data:image/jpg;base64,xxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
xxxxx = base64 encoded string of images bytes
This sounds like a good idea, and one way to do it is something like this:
After taking the image with camera you should get back an imageUri of the photo.
then:
1.Get the input stream of that image:
InputStream stream = getContentResolver().openInputStream(imageUri);
2.Fill in the stream into a ByteArrayOutputStream:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fillStream(stream, baos); // dummy method, provide implementation
3.Convert the byte array to Base64:
String base64Image = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
Then you can use the base64 string as the data for your image in HTML.
Related
I have a photo (jpeg) format in an Android device. Instead of posting the file as a file using HTTP I prefer to convert that to a string and post it as string using http to a spreadsheet. I understand jpeg files are encoded and opening them as string shows funny characters. My question is if I send these characters as string using http, can I get them back on the other side using a binary file editor and save them as jpeg?
One thing you can do is, on client side:
1.Convert image to byte array.
Path path = yourImageFile.toPath();
byte[] byteArray = Files.readAllBytes(path);
2.Encode the byteArray to Base64 String.
String encodedString = Base64.encodeBase64URLSafeString(byteArray);
3.That's it, send via http.
I'm not really sure what you mean by a binary file editor, but from server side you can retrieve the image like this (if using Java):
byte[] decodedByte = decodedBase64.decodeBase64(encodedString);
FileOutputStream out = new FileOutputStream("newPathToYourImage");
out.write(decodedByte);
out.close();
I am having an issue to parse an image within HTML structure to a PDF file, I have tried using the following line to gather the image , however the PDF file is being created without the image. I am using iText with xmlworker libraries, the html is all stored in a string then parsed into an input stream as shown below in code.
.....
<img class='top' src='file:///android_res/drawable/logo.png' height='100px' width='200px'/>
........
........
InputStream is = new ByteArrayInputStream(str.getBytes()); //contains the html string
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
You should probably use your own ImageProvider to fetch the images. (http://api.itextpdf.com/xml/com/itextpdf/tool/xml/pipeline/html/ImageProvider.html)
This interface has 4 methods:
getImageRootPath()
reset()
retrieve(String src)
store(String src, Image img)
You'll need to implement the retrieve method to return the Image from your file system.
Documentation on XMLWorker: http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html#itextdoc-menu-10
Short question: Can a html img tag read a base64 encoded image file?
<img src='path-to-file/image.base64'>
Because I'm having trouble. It fires the onerror event but doesn't give a reason (well at least it doesn't say it couldn't find the file)
Oh! Some context: I'm trying to cache images to sdcard on android as base64 encoded files. That part works, but when I try to load the cached image it fails :(
The syntax to source the image from inline data is:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="your image">
Okay it turns out that base64 is only for inline use (or if you have the string you can set it with javascript)
So this works:
var image = new Image();
image.src = base64_string;
But it doesn't work when you point the src to a file containing the base64_string
var image = new Image();
image.src = "path/base64.txt";
PS: Thanks for the comments, this pointed me in the right direction
I want to send Image from Android to Server. I decoded image into Base64 String and send it to the server. I use following code to convert Image to String
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] byteArray = bao.toByteArray();
String imageToString=Base64.encodeToString(byteArray,Base64.DEFAULT);
return imageToString;
Now i am unable to convert it back to Image on server side. I tried this
byte[] imageBytes=Base64.decode(imageString);
InputStream in = new ByteArrayInputStream(imageBytes);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "jpg", new File("D:\\myImage.jpg"));
i am getting Bogus Huffman table definition exception and sometime im = null exception. plz tell me what mistake i am making
Edit: Error Message javax.imageio.IIOException: Bogus Huffman table definition at this line
BufferedImage bImageFromConvert = ImageIO.read(in);
Try this
byte[] imageBytes=Base64.decode(imageString,Base64.NO_WRAP);
InputStream in = new ByteArrayInputStream(imageBytes);
Bitmap b = BitmapFactory.decodeStream(in);
Well there might be multiple issues here. The first one I think is the fact that you convert the image bytes to String (encoding them with whatever default encoding you Android environment has) and the decoding that String back to bytes without ensuring that you use the same text encoding (and thus get the same bytes).
Why not send the bytes directly? Or better yet just upload the file directly via HTTP multi-part form. There's a tutorial on this here:
http://flo.dauran.com/194-android-uploader-une-image-sur-une-serveur-web/
(it's in french, but there's detailed code examples)
Simply put I need to capture image using camera and upload it to facebook via my android application. And I successfully did that. The problem is when the photo posted in facebook, it's just too small and in low resolution while the image I took is in high resolution.
I understand that: in order to upload to facebook, i need to convert the captured image which is in bitmap format into byte array. So i have method for that:
public static byte[] convertBitmapToByteArray(Bitmap bm){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.PNG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
return bitmapdata;
}
Then to upload the image to facebook, i have code below where byteData is byte array I converted from bitmap image using the method above.
parameters.putString("message", "Test");
parameters.putByteArray("source", byteData);
String facebookResponse = facebookInstance.request(albumId+"/photos",parameters,"POST");
return facebookResponse;
I am pretty sure the problem is my convertBitmapToByteArray method since the method is to compress the bitmap image and turn it into byte array, and this made my image into low resolution image. However I can't seem to find the way to upload the image without converting it into byte array first. Any solution for me?
Alright even this thread is old, i found out the answer. It's not the problem of CompressFormant.JPEG or CompressFormat.JPG. Simply put, intent in android isn't designed to carry big data like image from activity through activity. I need to save the image from intent to sd card first before able to pull it out from there. It's my solution.