i find it supports well in Java,i can convert it to jpg;
but when i use it in Android,bufferimage,graphics,imageio are not support
becuase android drop the java.awt
so if i want to use poi in android,how can i do
tell me something useful, thks.
swamy gave you rather useful link.
as of bufferedImage, you can adopt it and write your own adapter, as I did:
BufferedImage image = ImageIO.read(url);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( image, "jpeg", baos );
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
int idx = ppt.addPicture(imageInByte, XSLFPictureData.PICTURE_TYPE_JPEG);
XSLFPictureShape pic = xslfSlide.createPicture(idx);
I got docx4j (which can handle pptx) running on Android; see jaxb-can-be-made-to-run-on-android
The reason I mention this, is I had to overcome java.awt issues, which might help you. I repackaged as https://github.com/plutext/ae-awt
If you want to use that with POI, you'd have to alter the references in POI.
If you are just using pptx, you might find it easier to use docx4j (since with POI, you'll probably also need to get XML Beans working on Android).
Related
I want to add Pie chart in my PDF using itext in Android.
But all the example related to piechart seems to use the following 2D classes:
Graphics2D graphics2d = template.createGraphics(width, height,
new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
height);
that are actually awt classes and not available in android version of itext-5.
Any alternate to using 2D classses ?
Edited: I am using itextg version for android. Its missing 2D classes mentioned above.
So i have found a solution.
I was using MPAndroidChart for making the PieChart.
This library has a method for getting the bitmap from the drawn Piechart.
pieChart.getChartBitmap();
So, once you get the bitmap, iText will let you add the bitmap into your pdf.
The final code may looks like this:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = pieChart.getChartBitmap();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Image myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
document.add(myImg);
Cheers!
For Android you need to use itextg instead of itextpdf.
Examples that use classes that are not available on Android, should not be used. When you find a solution, it would be nice if you submitted your documentation.
I have been going crazy with this android error and from browsing the previous posts about this error none of the solutions that were given helped me.
I am of course talking about the all powerful killer known as the 'android Out of memory on a (NUMBER)-byte allocation.' I am trying to create a photo uploader and after the 3rd maybe 4th image my app crashes. I know to stop the crash to use a catch exception however that is not my problem. I am here to ask the community is there any solution to fixing this byte allocation error ?
Here is a snippet of my code involving the bit map .
String post = editTextPost.getText().toString().trim();
// get the photo :
Bitmap image = ((BitmapDrawable)postImage.getDrawable()).getBitmap();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// compress the image to jpg format :
image.compress(Bitmap.CompressFormat.JPEG, 45, byteArrayOutputStream);
byte[] imageBytes = byteArrayOutputStream.toByteArray();
String encodeImage = Base64.encodeToString(imageBytes,Base64.DEFAULT);
// Recycle bitmap :
image.recycle();
image=null;
// send data :
sendPost p = new sendPost(this);
p.execute(post, encodeImage);
imageBytes=null;
Im not using any libraries and would like to keep it that way however if using a library is the only option I will use one. Hopefully someone can help.
Bitmaps won't completely recycle if they are attached to a View, for example to a ImageView, if you do imageView.setImageBitmap(bitmap), you need to clear it before doing imageView.setImageBitmap(null) and clearing any other reference to any view.
After you finish uplloading the image release the memory occupied by the "postImage" -
postImage.setImageDrawable(null);
This will release any memory occupied by the bitmap associated with postImage
General Answer:
When you upload files you need to open a stream to your server and to
your file at same time. read and write chunk by chunk of 1 MB for
example. this way you will not get an out of memory exception.
I'm trying to use a very simple piece of code that has been supported since API 1.
if (bitmap != null) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.WEBP, IMAGE_QUALITY, byteArrayOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray();
result = Base64.encodeToString(bytes, Base64.DEFAULT);
}
somehow,
bitmap.compress(Bitmap.CompressFormat.WEBP, IMAGE_QUALITY, byteArrayOutputStream);
gives me that weird error: java.lang.NoSuchFieldError: android.graphics.Bitmap$CompressFormat.WEBP and this only happens on my moto razr 2.3
i can't find anything on the internet about this. what's going on? (i get that, clearly, motorola's stock android didn't think to include it, but how would i fix this?) any hints?
all i want to do is to compress take a snapshot of the screen (or view), and attach it IN A USER FRIENDLY WAY in String form (this same device also doesn't do file attachments correctly either) to the body of an email intent, and pass that off to whoever can send emails.
trying to compress to .jpeg, then converting it to base64 encoded string, and then attaching to email in the body takes forever, and is not very user-responsive. this can't happen.
any help?
android.graphics.Bitmap$CompressFormat.WEBP only works for api level 14 or above.
Try some WebP libraries.
WebP library for java?
I know it is possible to save bitmap to png or jpg
now i want to save bitmap to .tiff,anybody can help?
Android does not have native support for saving TIFF files.
You best bet would be to get the raw byte array, and then using either a Java or NDK library (like libtiff) to save it as a TIFF.
I believe this is is a repost from here:
Convert a bitmap image to an uncompressed tif image in Java
To answer the question, the above answer suggested the following using the Java Advanced Imaging (JAI) library:
TIFFImageWriterSpi spi = new TIFFImageWriterSpi();
ImageWriter writer = spi.createWriterInstance();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_DISABLED);
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("output.tif"));
writer.setOutput(ios);
writer.write(null, new IIOImage(bmp, null, null), param);
You always can save the Bitmap as .png and convert it to .tiff
Android hasn't support for tiff images. You should use some library for working with tiff. For example my: https://github.com/Beyka/Android-TiffBitmapFactory
Might sound crazy, but it's what I need to do. I want to take a Bitmap object and use the XMLPullParser/XmlSerializer to write this to a flat file. Obviously I will need to read the XML tag back into a Bitmap object.
I have tried various things, similar to how I write and read Bitmaps from a database.
Bitmap bitmap = ((BitmapDrawable) icon).getBitmap();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, outputStream);
byte[] bitmapByte = outputStream.toByteArray();
Where I get lost is once I write this out to my XML file as a String, I can never get it converted back properly. Any pointers is appreciated.
Edit:
I want to provide a little more information. I am writing out a good deal of XML data for a backup purpose, so loading or writing time is of no concern. This is not a main source of the data (main source is a SQLite database). I do not want to have to write out a couple of very small (48x48pixel) images as well if I can help it.
I am reading in my XML using the XMLPullParser, which reads a String:
if (name.equalsIgnoreCase("someXmlTag")){
String someString = parser.nextText();
I am writing out my XML using the XmlSerializer, which writes a String:
XmlSerializer serializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
serializer.setOutput(writer);
serializer.startDocument("UTF-8", true);
serializer.startTag("", "someTag");
serializer.text(someString);
So somehow I have to turn my Bitmap into a String and then turn that String back into a Bitmap. I will do some searches on Base64 to see if I get any good examples.
I would suggest using something like Base64 encoding.
The XMLPullParser is, as its name suggests, a parser, it's not used to write XML. Your best bet is to store it as Base64 like mentioned before.
Needless to say, this is going to take a lot of space for no good reason.
It will also be a lot slower to read back.