File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Android and the fly likes problem in converting string to byte array. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "problem in converting string to byte array." Watch "problem in converting string to byte array." New topic

problem in converting string to byte array.

Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 632

Hello everybody,

I am developing an application in that I want to upload image to the server. If image is less than 900 kb then application run fine. but when image is more than 900kb then it throws exception as java.lang.OutOfMemoryError.

my code is as follows

I have Base64 class which has method

What is wrong here?

Thanks in advance.

Life is easy because we write the source code.....
Ankit Garg

Joined: Aug 03, 2008
Posts: 9452

I would say your code is not properly created for mobile devices. You are reading the whole file into a byte array, then encoding it with Base64 to get a String and then again convert the whole String back to byte array. This will use a lot of memory for a mobile device to handle. Why don't you read the file in chunks like 100bytes at a time and send it to the output stream. That way you won't use that much memory...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
I agree. Here's the link:
subject: problem in converting string to byte array.
It's not a secret anymore!