Android - java.lang.NoSuchFieldError: android.graphics.Bitmap$CompressFormat.WEBP - android

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?

Related

Android : Bitmap causing out of memory error

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.

How to store image in Database?

I need to store in Database images chosen from gallery. My firt idea was convert Bitmap to String and store String in Database, but now I am reading other post: saving image clicked from camera or loaded from gallery in database and retrieving it and there is suggested using byte array.
Could someone explain me diffrence, which idea is better? Maybe something else?
I just start, but I would like to write it possibly correctly.
The standard way to store an image as a byte[] in a BLOB field. Another possibility - with some overhead - is to store a Base64-encoded string.
You can use the Base64 Android class:
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
You'll have to convert your image into a byte array though. Here's an example:
Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
If you're using an older SDK library (because you want it to work on phones with older versions of the OS) you won't have the Base64 class packaged in (since it just came out in API level 8 aka version 2.2).
Check this article out for a work-around:
http://androidcodemonkey.blogspot.com/2010/03/how-to-base64-encode-decode-android.html

How can i use poi to read ppt?

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

Out of memory on a byte allocation (Bitmap as String to webservice using soap)

Am having a bitmap , so I want to upload a webserivceas string and want to retrive the string.
For converting bitmap to string am using:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
strBase64 = Base64.encodeToString(byteArray, Base64.URL_SAFE);
this above String is using as property to in soapobject to upload.
But am getting Out of memory on a 11674900-byte allocation, while print and uploading.
And if i debugged the issue, without printing am getting
com.sun.jdi.InvocationException occurred invoking method.
on soaprequest.
How to resolve this issue and to upload image to webservice as string ?
You are creating 3 copies of an 11MB image(bitmap, stream, strBase64). So reduce the memory usage by calling
bitmap.recycle();
below this line:
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
Also close the stream when you are done with it(below stream.toByteArray();):
stream.close();
stream = null;
Remember that there is no guarantee that memory will be cleaned immediately after these calls. Proper way to handle this type of situation is to transfer large files chunk by chunk.
An 11 Million byte allocation much larger than most phones' heap can handle. you definitely don't want to be holding a byte array of that size in memory.
Try using insample size with
BitmapFactory.decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts)
and settings options to use insample size to return a reasonably sized image.
A simple fix for some might be to add android:configChanges="orientation|screenSize" to your manifest. In my case, the Nexus_S emulator was crashing without this line, while the actual Nexus 7 device I was testing on wasn't crashing on rotation.
Adding this appears to be an easy fix for apps that have a couple large "match_parent" bitmaps to rotate and resize.
Careful if you are building for APIs before 13!

The image is too small when I try to upload it using Facebook Graph API and Android

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.

Categories

Resources