LeandroC
LeandroC

Reputation: 131

How to Get an Audio File and decode to a base64 String?

I need to take an audio file from my SD card (I have the path), but How can I convert to an base64 to put It in a String. I need it to pass to an Web Service.

Upvotes: 0

Views: 4793

Answers (2)

rahul
rahul

Reputation: 2661

try this

    private void doFileUpload(){
     byte[] videoBytes;
     try {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         FileInputStream fis = new FileInputStream(new File(selectedPath));

         byte[] buf = new byte[1024];
         int n;
         while (-1 != (n = fis.read(buf)))
             baos.write(buf, 0, n);

        String videoBytes = baos.toByteArray();


         video_str = Base64.encodeBytes(videoBytes);
            System.out.println("video array"+video_str);


    } catch (Exception e) {
        // TODO: handle exception
    }}

and download Base64 class from org.apache.ws.commons.util.Base64 or from http://iharder.net/base64

Upvotes: 4

Toilal
Toilal

Reputation: 3409

Use org.apache.ws.commons.util.Base64 class, from apache ws-commons-utils

Upvotes: 0

Related Questions