简体   繁体   中英

Unable to create new directory inside the Gallery folder

I am making an app which allows users to record audio clips and access them. I am trying to save the recorded audio files inside a custom folder in my gallery.

I have a function that creates a File and sets its location:

private File getOutputMediaFile(int type) {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "dirName" + File.separator);
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_" + timeStamp + ".jpg");
        } else if (type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_" + timeStamp + ".mp4");
        } else if (type == MEDIA_TYPE_AUDIO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "AUD" + timeStamp + ".WAV");
        }
        else {
            return null;
        }
        return mediaFile;
    }

I use getOutputMediaFile to save recorded audio file like so:

audioRecorder = new MediaRecorder();
audioFilename = getOutputMediaFile(MEDIA_TYPE_AUDIO).getAbsolutePath();
//more code
audioRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_AUDIO).getAbsolutePath());

When I log audioFilename , I get the following path: /storage/emulated/0/DCIM/folderName/AUD20160425_172620.WAV

I am able to save and play the audio file, but with a caveat:

  1. The file is NOT stored in gallery/myFolder ; I can only view the audio file if I run the default play music app and look inside "my playlists".

I have the following permissions in my manifest:

 <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

What am I doing wrong?

I guess we need to tell MediaStore that "a new file is created". Try this:

public void refreshGallery(File file, Context context) {
    Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(file);
    scanIntent.setData(contentUri);
    context.sendBroadcast(scanIntent);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM