pratibha
pratibha

Reputation: 113

How to get a an audio file from android and save it as a file in server instead of database using webservice-j2ee

i want to pass an audio file from android project to dynamic web application project in java. now instead of creating database as backend i want to create a folder in server to save this audio file.... can anyone plz help me how to save a .mp3 file in a folder in server... my code is as follows

package com.android.audio.mediaplayer;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.android.qrcode.db.AudioVideoServer;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;



public class AudioRecord  extends Activity
{
    private static final String LOG_TAG = "AudioRecordTest";
    private static String mFileName = null;
    public String AudioFile;

    private RecordButton mRecordButton = null;
    private MediaRecorder mRecorder = null;

    private PlayButton   mPlayButton = null;
    private MediaPlayer   mPlayer = null;
    private SubmitButton mSubmitButton = null;
    private String url = "postLoginData.do?do=postLoginData";
    String result;
    byte[] value;
    String s;

    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            startPlaying();
        } else {
            stopPlaying();
        }
    }

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }
    /* public boolean saveas(int ressound){  
             byte[] buffer=null;  
             InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
             int size=0;  

             try {  
              size = fIn.available();  
              buffer = new byte[size];  
              fIn.read(buffer);  
             fIn.close();  
            } catch (IOException e) {  
             // TODO Auto-generated catch block  
            return false;  
            }  

            String path="/sdcard/media/audio/ringtones/";  
            String filename="examplefile"+".ogg";  

            boolean exists = (new File(path)).exists();  
            if (!exists){new File(path).mkdirs();}  

            FileOutputStream save;  
            try {  
             save = new FileOutputStream(path+filename);  
             save.write(buffer);  
             save.flush();  
             save.close();  
            } catch (FileNotFoundException e) {  
             // TODO Auto-generated catch block  
             return false;  
            } catch (IOException e) {  
             // TODO Auto-generated catch block  
             return false;  
            }      

            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  

            File k = new File(path, filename);  

            ContentValues values = new ContentValues();  
            values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
            values.put(MediaStore.MediaColumns.TITLE, "exampletitle");  
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
            values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");  
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);  
            values.put(MediaStore.Audio.Media.IS_ALARM, true);  
            values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

            //Insert it into the database  
            this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);  


            return true;  
           }  */
    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

    class RecordButton extends Button {
        boolean mStartRecording = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onRecord(mStartRecording);
                if (mStartRecording) {
                    setText("Stop recording");
                } else {
                    setText("Start recording");
                }
                mStartRecording = !mStartRecording;
            }
        };

        public RecordButton(Context ctx) {
            super(ctx);
            setText("Start recording");
            setOnClickListener(clicker);
        }
    }

    class PlayButton extends Button {
        boolean mStartPlaying = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onPlay(mStartPlaying);
                if (mStartPlaying) {
                    setText("Stop playing");
                } else {
                    setText("Start playing");
                }
                mStartPlaying = !mStartPlaying;
            }
        };

        public PlayButton(Context ctx) {
            super(ctx);
            setText("Start playing");
            setOnClickListener(clicker);
        }
    }
    class SubmitButton extends Button {

        OnClickListener clicker = new OnClickListener() {

            public void onClick(View v) {

                byte[] file = mFileName.getBytes();
                System.out.println("$$$$$$$$$$$" + file);
                s = Base64.encodeToString(file, MODE_APPEND);
                System.out.println("**************" + s);

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("Audiofile.mp3", s));
                result = AudioVideoServer.executePost(url, nameValuePairs);

            }
        };

        public SubmitButton(Context ctx) {
            super(ctx);
            setText("Save");
            setOnClickListener(clicker);
        }
    }

    public AudioRecord() {
        Bundle b = getIntent().getExtras();
         //mImagePath = b.getString("path");
        AudioFile = b.getString("audiofile");

        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName +=AudioFile; 
            //"/audiorecordtest.mp4";
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        LinearLayout ll = new LinearLayout(this);
        mRecordButton = new RecordButton(this);
        ll.addView(mRecordButton,
            new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));
        mPlayButton = new PlayButton(this);
        ll.addView(mPlayButton,
            new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));
        mSubmitButton = new SubmitButton(this);
        ll.addView(mSubmitButton, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, 0));


        setContentView(ll);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
}

Upvotes: 3

Views: 1844

Answers (1)

kgiannakakis
kgiannakakis

Reputation: 104168

An easy way to implement file upload functionality in a Java EE application is to utilize the restlet framework, which has an extension for file uploading. This extension is based on Apache's FileUpload.

You can find some examples for FileUpload here and here.

If you are using Tomcat 7 or any other Servlet 3.0 container, then file uploading functionality is built-in. See here for an example.

Upvotes: 2

Related Questions