aspose file tools*
The moose likes Java in General and the fly likes Reading image and convert it to Base64 format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading image and convert it to Base64 format" Watch "Reading image and convert it to Base64 format" New topic
Author

Reading image and convert it to Base64 format

Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

I am developing one android application.in this application i am reading one image from phone and convert that image to Base64 format and save it in one txt file.



Here String base64String contains image in txt format. Now I want to write this string into one txt file. I had write one method for this



But here txt file only write first chunk of data correct, (i.e data of byteArray[102400] bytes ) after that data is not written correctly. I think I am making some mistake in writing the file, but I don't know what?

Thanks


Life is easy because we write the source code.....
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Here , in txt file it only write first 102400 byte of data. My image is 133kb, then it needs two chunks of 102400 bytes, but in txt file it write first chunk of data. Because I think I had write
pw.flush();
pw.close();
therefore it only write first chunk of data, and can't write second chunk of data.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Then don't close pw in the writeFile method, or at least make it optional:
Instead of calling writeFile(base64String) in that loop you call writeFile(base64String, false), then call pw.close() after the loop has finished.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Thanks Rob, for your response, but still it is not giving me correct output.
If I passed true to writeFile(String data, boolean close) then it only print first chunk of data and if I passed false then it will write that first chunk of data again in txt file
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
The logic is screwy - the Base64.encode call takes the complete byteArray, even though that may not have been filled with data yet, and the "str" field isn't used. You can't base64-encode parts of the file and then concatenate those.

Instead, you should size the byte array to fit the length of the file, then fill it with data in the loop, then call base64 on it, and only then write it to the output file.


Ping & DNS - my free Android networking tools app
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

But I am developing this application for mobile , if image is upto 7-8 MB then it is not possible to create byte array of 7-8 MB
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
Well, that is how base64 works. But this begs the question: why do you want to base64-encode the image in the first place? What are you trying to do where that would be necessary?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Actually I want to store image on server and also want to restore that image from server to mobile. Is it possible to send image without converting it to Base64? if yes then how?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Same way as anything else--it's clear that not everything sent to a mobile device is in Base64, for example, I have a web browser on my phone that sends and receives standard HTTP requests.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
The only benefit base64 brings is that the result is ASCII, not binary; why would that make a difference for storing and transferring a file?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Thank you David and Ulf for your help, right now I can't do that But I will try that tomorrow. And I will ask again If I have some difficulty. Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

For storage and transferring, base64 is less efficient as direct byte arrays. First of all, you need to encode and decode the base64 string. Second, in base64 there are only 64 different values possible, each taking up a byte. With bytes you have 255 different values. It doesn't take a genius to see that to map 255 different values onto 64 different values you will need more of the latter. So a base64 string will never be smaller than the byte array. The little example on the wiki already shows this: "Man" gets transformed into "TWFu", adding a full character.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Hello once again,

I had tried to send the image without converting into Base64 format, but on server side they can't read that image, so there is any other way to send image to server. I can't change the server side logic. And this application is for mobile so I can't create a byte array of image size, So what to do?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
If the server can't process what it's receiving, then apparently it's expecting something other than what your code is sending. What *does* the server expect? Surely you have documentation or examples available that tell you the details about that?

And this application is for mobile so I can't create a byte array of image size

Possibly, but 100KB is very little memory. What kind of device are we talking about that doesn't have that much available? But regardless, it's still possible to send the image data byte by byte even if you can't allocate a 100KB byte array. The details, of course, depend on what exactly the server expects, and that you haven't told us.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Hello Ulf,

I am developing this application for Android, I can create array upto 700kb. In one module of this application I sent a 2mb vcf file of to server by using 100kb of byte array.
Now in this image uploading module I want to send a image to server if I send a image file to server by a 100 kb of byte array then server can't read that file, using the same code if I send a txt file then it can read that file. It also read a Base64 format file. But not image without converting into Base64.

Now I want to read that image into parts suppose image size is 1mb . I want to read 100kb of that file and send it to the Basse64 class.(here Base64 class has method - public static String encode(byte[] raw) {...}) Now I want to send that string which is return from encode method to the server. After sending that string to the server, I want to read next 100kb of image from that file and pass to that method which convert it to Base64 format. continue while whole image is not converted into Base64 format.

But my problem is that it reads first 100kb of byte array and convert it into Base64 successfully, but in the next loop it not gives the correct output. how to read next 100 kb of byte array? Is it possible on server side to decode that chunks of data and join them to produce a Image? I think it will not possible. If It is not possible then How to send big image to server by using 100 kb of byte array?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
I'm confused. Earlier you said you can't change the code on the server; but now it's possible? Which is it?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

I mean it accept a txt file data and Base64 format data. But when I read image by using



it can't read the value of str . But when I convert that str to base64 format then it can read.




Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
This code ignores the question of encodings - the platform default encoding is likely to be different between a mobile device and the server. You should use the String(byte[], int, int, String) constructor instead of String(byte[], int, int).

Not knowing what the sendData method does, it's impossible to tell what other problems there may be.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Here is code for sendData method

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
What happens when you run that code? "it can't read the value of str" doesn't tell us anything about what happens on the server. Also, you're not really ignoring exception on the client, are you? You can't do that.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

when i convert image to Base64 format then sendData(String str) method execute properly till the end. (means statement System.out.println ("at the end of try block"); is executed

But if I comment the line i.e if it is not convert into Base64 format then sendData(String str) method not executed till the end. It prints only upto statement.

This statement never get executed

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
That sounds as if an exception occurs that the code is unfortunately ignoring due to the empty catch blocks. As I said, don't do that.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

After catching the exception it gives me following exception




The exception occurs at this line But I don't understand why?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
Sounds like a networking problem. Have you made sure that the client can access that server using a browser?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

But it will not throw any exception if string pass to the sendData(str) method is in Base64 format. But don't know why it throws FileNotFoundException. please don't look at NullPointerException it is due to I had made some changed in code, so only try to solve why FileNotFoundException are thrown
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading image and convert it to Base64 format