Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6072835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:12:14+00:00 2026-05-23T10:12:14+00:00

I have been stuck on this issue for a few hours now trying to

  • 0

I have been stuck on this issue for a few hours now trying to get it working. Basically what I am trying to do is the following. Base64 encode an audio file picked up from an sdcard on an Android device, Base64 encode it, convert it into a String and then decode the String using Base64 again and save the file back to sdcard. It all sounds fairly simple and works great when doing it using a text file. For example if I create a simple text file, call it dave.text and inject some text it in say “Hello Dave” or something similar it works great, but fails when I try doing the same with a binary file, audio in this example. Here’s the code that I am using.

File file = new File(Environment.getExternalStorageDirectory() + "/hello-4.wav");
byte[] FileBytes = FileUtils.readFileToByteArray(file);

byte[] encodedBytes = Base64.encode(FileBytes, 0);
String encodedString = new String(encodedBytes);                                        
Utilities.log("~~~~~~~~ Encoded: ", new String(encodedString));

byte[] decodedBytes = Base64.decode(encodedString, 0);
String decodedString = new String(decodedBytes);
Utilities.log("~~~~~~~~ Decoded: ", new String(decodedString));

try {
    File file2 = new File(Environment.getExternalStorageDirectory() + "/hello-5.wav");
    FileOutputStream os = new FileOutputStream(file2, true);
    os.write(decodedString.getBytes());
    os.flush();
    os.close();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

At this point if I try to save the file, it becomes corrupted. The audio file hello-5.wav is larger in size then the original and it doesn’t play.

Any ideas what I am doing wrong here? If I try to save decodedBytes using os.write(decodedBytes) it works but not when converted to a String and getBytes() is used.

Any ideas? Thank You!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:12:15+00:00Added an answer on May 23, 2026 at 10:12 am

    You’re heavily mixing Strings and byte[]s. Don’t do that. If you want to encode a byte[] to a String, use Base64.encodeToString(), instead of encoding to bytes and then creating a string.

    If I try to save decodedBytes using os.write(decodedBytes) it works but not when converted to a String and getBytes() is used.

    Calling new String(byte[]) does not do what you think it does.

    Likewise use Base64.decode(String, int) to decode the string.

    Something like this (not tested):

    File file = new File(Environment.getExternalStorageDirectory() + "/hello-4.wav");
    byte[] bytes = FileUtils.readFileToByteArray(file);
    
    String encoded = Base64.encodeToString(bytes, 0);                                       
    Utilities.log("~~~~~~~~ Encoded: ", encoded);
    
    byte[] decoded = Base64.decode(encoded, 0);
    Utilities.log("~~~~~~~~ Decoded: ", Arrays.toString(decoded));
    
    try
    {
        File file2 = new File(Environment.getExternalStorageDirectory() + "/hello-5.wav");
        FileOutputStream os = new FileOutputStream(file2, true);
        os.write(decoded);
        os.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    

    But why are you base64 encoding an audio file in the first place?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been stuck with this issue for a few days now, and really need,
So I have been stuck for a couple of hours trying to get this
so i have been stuck with this issue for 3 weeks now and i
I have been stuck with this issue now all morning. I actually saw this
I have now been stuck at this for some time so I though to
Hope to get solution to this problem. I have been stuck on it since
I've been stuck with this for weeks now and have no idea where I'm
I have been stuck with this problem for a while now, and I just
I've been stuck on this for a few days now and can't seem to
I've asked a few questions which have touched around this issue, but I've been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.