简体   繁体   English

无法在Gallery文件夹中创建新目录

[英]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: 我使用getOutputMediaFile保存录制的音频文件,如下所示:

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 当我登录audioFilename ,得到以下路径: /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 ; 该文件未存储在gallery/myFolder I can only view the audio file if I run the default play music app and look inside "my playlists". 如果运行默认的play music应用并查看“我的播放列表”,则只能查看音频文件。

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". 我想我们需要告诉MediaStore“已创建一个新文件”。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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