安卓保存音频文件到内部存储音频文件

2023-09-07 01:46:38 作者:追梦ゝ

目前,我已经创建了音频记录Android应用程序。该文件被保存在外部存储。

如果我改变的方法保存为内部存储与MODE_PRIVATE的背景下

我看了一下文件保存的Andr​​oid开发者,但我不知道如何将文件实例转换成字节数组保存文件并加载音频文件

下面是我的code保存音频文件

 公共无效的onClick(视图查看)抛出异常{
    如果(计数== 0){

        tbxRecordStatus.setText(记录);
        btnRecord.setText(停止记录);

        Toast.makeText(MainActivity.this,录制开始,
                Toast.LENGTH_SHORT).show();
        dateInString =新的SimpleDateFormat(YYYY-MM-DD-HH-MM-SS)。格式(
                新的日期())的toString()。
        字符串文件名=TVSSSSD_+ dateInString +record.3gp;

        SDCardpath = Environment.getExternalStorageDirectory();
        myDataPath =新的文件(SDCardpath.getAbsolutePath()
                +/.My录音);

        // MYDIR = context.getDir(媒体,Context.MODE_PRIVATE);
        如果(!myDa​​taPath.exists())
            myDataPath.mkdir();


        的AudioFile =新的文件(myDataPath +/+文件名);

        Log.d(路径,myDataPath.getAbsolutePath());
        //文件fil​​eWithinMyDir =新的文件(MYDIR,文件名);
        //的AudioFile = fileWithinMyDir;
        // FileOutputStream中出=新的FileOutputStream(fileWithinMyDir);

        记录=新MediaRecorder();
        recorder.reset();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB);
        recorder.setAudioEncodingBitRate(16);
        recorder.setAudioSamplingRate(44100);

        recorder.setOutputFile(audiofile.getAbsolutePath());

        。录音机prepare();

        recorder.start();

        算上++;

    } 其他 {

        tbxRecordStatus.setText(停止);
        btnRecord.setText(开始记录);
        Toast.makeText(MainActivity.this,停止录制,
                Toast.LENGTH_SHORT).show();
        如果(录像机!= NULL){
            recorder.stop();
            recorder.release();

            记录= NULL;

        } 其他 {
            tbxRecordStatus.setText(警告!);
            Toast.makeText(MainActivity.this,实录一,
                    Toast.LENGTH_SHORT).show();
        }
        计数= 0;
    }
}
 

下面是我的code加载音频文件

  recordList =新的ArrayList<字符串>();
    文件目录= Environment.getExternalStorageDirectory();
    文件=新的文件(目录+/我的录制);
    文件列表[] = file.listFiles();
    的for(int i = 0; I< list.length;我++){
        recordList.add(名单[I] .getName());
    }

    Collections.sort(recordList,Collections.reverseOrder());

    列表视图=(ListView控件)findViewById(R.id.listView);
    ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,
            android.R.layout.simple_list_item_1,android.R.id.text1,
            recordList);


    listview.setAdapter(适配器);

    listview.setTextFilterEnabled(真正的);

    listview.setOnItemClickListener(新OnItemClickListener(){

        @覆盖
        公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,
                长ARG3){
            // TODO自动生成方法存根


            字符串产品=((TextView中)查看).getText()的toString()。
            字符串fullPathAudio = file.getAbsolutePath()+/+的产品;

            文件resultFile =新的文件(fullPathAudio);

            意向意图=新的意图();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(resultFile),音频/ *);
            startActivity(意向);
        }
    });
 
安卓酷我音乐的歌词文件存储位置

解决方案

使用 getFilesDir()

  SDCardpath = getFilesDir();
        myDataPath =新的文件(SDCardpath.getAbsolutePath()
                +/.My录音);

        // MYDIR = context.getDir(媒体,Context.MODE_PRIVATE);
        如果(!myDa​​taPath.exists())
            myDataPath.mkdir();


        的AudioFile =新的文件(myDataPath +/+文件名);
 

欲了解更多请参见使用内部存储

Currently I have created the audio-recording Android App. The file is saved at External Storage.

What if I change the method to save as Internal Storage with the context of MODE_PRIVATE

I have read the Android developers about saving files but I have no idea how to convert the file instance into the byte array to save the file and load the audio file

the below is my code to save the audio file

public void onClick(View view) throws Exception {
    if (count == 0) {

        tbxRecordStatus.setText("Record");
        btnRecord.setText("Stop Record");

        Toast.makeText(MainActivity.this, "Recording Starts",
                Toast.LENGTH_SHORT).show();
        dateInString = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(
                new Date()).toString();
        String fileName = "TVSSSSD_" + dateInString + " record.3gp";

        SDCardpath = Environment.getExternalStorageDirectory();
        myDataPath = new File(SDCardpath.getAbsolutePath()
                + "/.My Recordings");

        // mydir = context.getDir("media", Context.MODE_PRIVATE);
        if (!myDataPath.exists())
            myDataPath.mkdir();


        audiofile = new File(myDataPath + "/" + fileName);

        Log.d("path", myDataPath.getAbsolutePath());
        // File fileWithinMyDir = new File(mydir, fileName);
        // audiofile = fileWithinMyDir;
        // FileOutputStream out = new FileOutputStream(fileWithinMyDir);

        recorder = new MediaRecorder();
        recorder.reset();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setAudioEncodingBitRate(16);
        recorder.setAudioSamplingRate(44100);

        recorder.setOutputFile(audiofile.getAbsolutePath());

        recorder.prepare();

        recorder.start();

        count++;

    } else {

        tbxRecordStatus.setText("Stop");
        btnRecord.setText("Start Record");
        Toast.makeText(MainActivity.this, "Recording Stops",
                Toast.LENGTH_SHORT).show();
        if (recorder != null) {
            recorder.stop();
            recorder.release();

            recorder = null;

        } else {
            tbxRecordStatus.setText("Warning!");
            Toast.makeText(MainActivity.this, "Record First",
                    Toast.LENGTH_SHORT).show();
        }
        count = 0;
    }
}

the below is my code to load the audio file

recordList = new ArrayList<String>();
    File directory = Environment.getExternalStorageDirectory();
    file = new File(directory + "/My Recordings");
    File list[] = file.listFiles();
    for (int i = 0; i < list.length; i++) {
        recordList.add(list[i].getName());
    }

    Collections.sort(recordList, Collections.reverseOrder());

    listview = (ListView) findViewById(R.id.listView);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1,
            recordList);


    listview.setAdapter(adapter);

    listview.setTextFilterEnabled(true);

    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long arg3) {
            // TODO Auto-generated method stub


            String product = ((TextView) view).getText().toString();
            String fullPathAudio = file.getAbsolutePath() + "/" + product;

            File resultFile = new File(fullPathAudio);

            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(resultFile), "audio/*");
            startActivity(intent);
        }
    });

解决方案

Use getFilesDir()

 SDCardpath = getFilesDir();
        myDataPath = new File(SDCardpath.getAbsolutePath()
                + "/.My Recordings");

        // mydir = context.getDir("media", Context.MODE_PRIVATE);
        if (!myDataPath.exists())
            myDataPath.mkdir();


        audiofile = new File(myDataPath + "/" + fileName);

For more see Using the Internal Storage