Search code examples
androidaudiorecorder

Trouble set location to recorded audio


I have to following code to launch and the audio recorder on Android:

final Intent recordSoundIntent = new Intent
("android.provider.MediaStore.RECORD_SOUND");
String fileName = Environment.getExternalStorageDirectory() +
File.separator + UUID.randomUUID() + ".3gpp";
recordSoundIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new
File(fileName)));
startActivityForResult(Intent.createChooser(recordSoundIntent,
getString(R.string.record_sound_chooser)), INTENT_SOUND_RECORDING);

My problem is the following:

My filename (fileName) has no effect, the Uri returned from data.getData() returns in my last test run: content://media/external/audio/media/41. However, this file is created on my sdcard: recording34485.3gpp. If it is not possible to set custom location upon creating sound it is the location to this file I would like.


Solution

  • Was working on the same problem. Could not find a way to set location directly but found a work around solution of getting and renaming it.

    Getting file name was dealt with here, file is then renamed using java.io.File.renameTo(). You'll need android.permission.WRITE_EXTERNAL_STORAGE in your manifest file for the rename to work.

    Here is my test code example:

    public static final int REQUEST_RECORD_SOUND = 7;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
    
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent  = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
                startActivityForResult(intent, REQUEST_RECORD_SOUND);
            }
        });
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
    
        if (requestCode == REQUEST_RECORD_SOUND){
            String sourcePath = getRealPathFromURI(intent.getData());
            File root = Environment.getExternalStorageDirectory();
            String destPath = root.getPath() + File.separator + "newName.txt";
            File sourceF = new File(sourcePath);
            try {
                sourceF.renameTo(new File(destPath));
            } catch (Exception e) {
                Toast.makeText(this, "Error:" + e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }
    
    /**
     * from:
     * https://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore
     */
    public String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }