简体   繁体   English

无法在Android的Gallery文件夹中显示新添加的媒体

[英]Unable to display newly added media inside Gallery folder in Android

I am able to successfully record the audio and play it back. 我能够成功录制音频并进行播放。 The file is being stored in the SD and has a filePath like so: /storage/emulated/0/20160516_104008 . 该文件正在存储在SD中,并具有一个文件/storage/emulated/0/20160516_104008如下所示: /storage/emulated/0/20160516_104008 The problem is that I want the user to be able to access it via the gallery widget. 问题是我希望用户能够通过图库窗口小部件访问它。

I researched this problem and this post recommended using MediaScannerConnection API, which I have implemented as follows: 我研究了此问题,并推荐使用MediaScannerConnection API来实现职位,该API已实现如下:

     MediaScannerConnection.scanFile(_reactContext,
                  new String[] { audioFileName }, null,
                  new MediaScannerConnection.OnScanCompletedListener() {
              public void onScanCompleted(String path, Uri uri) {
              }
        });  

Here is the relevant code: 以下是相关代码:

private boolean prepareAudioRecorder() {
        audioRecorder = new MediaRecorder();
        audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        audioFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        audioFileName += "/" + timeStamp;
        audioRecorder.setOutputFile(audioFileName);
        audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            audioRecorder.prepare();
            return true;
        } catch (IOException e) {
            Log.e(TAG, "prepare() failed");
            return false;
        }           
    }


public void stopAudioRecording(final Promise promise) {

        if (audioRecorderPromise != null) {
            audioFile = new File(audioFileName);
            releaseAudioRecorder();
            storeFile();
            promise.resolve("finished recording");
        } else {
            promise.resolve("not recording");
        }

private void releaseAudioRecorder() {
        if (audioRecorder != null) {
            audioRecorder.stop();
            audioRecorder.release();
             audioRecorder = null;
            if (audioRecorderPromise != null) {
                // audioRecorderPromise.resolve(Uri.fromFile(audioFile).toString());
                audioRecorderPromise.resolve(audioFileName);
                audioRecorderPromise = null;
            }   
        }       
    }

private void storeFile() {

        values = new ContentValues();
        values.put(MediaStore.Audio.Media.TITLE, audioFileName);
        values.put(MediaStore.Audio.Media.DATE_ADDED, System.currentTimeMillis());
        values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
        values.put(MediaStore.Audio.Media.DATA, audioFileName);
        ContentResolver cr =  _reactContext.getContentResolver();
        cr.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
        Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Uri newUri = cr.insert(base, values);
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);         
        intent.setData(Uri.fromFile(audioFile));
        _reactContext.sendBroadcast(intent);        
    }

You should not provide the file name but the absolute path 您不应提供文件名,而应提供绝对路径

Refer Android Developers 推荐Android开发人员

Update Better use MediaScannerConnectionClient: 更新最好使用MediaScannerConnectionClient:

private MediaScannerConnectionClient mediaScannerConnectionClient = new MediaScannerConnectionClient() {

    @Override
    public void onMediaScannerConnected() {
        mediaScannerConnection.scanFile("pathToFile/someName.extension", null);
    }

    @Override
    public void onScanCompleted(String path, Uri uri) {
        if(path.equals("pathToFile/someName.extension"))
            mediaScannerConnection.disconnect();
    }
};
MediaScannerConnection mediaScannerConnection = new MediaScannerConnection(context, mediaScannerConnectionClient).connect();

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

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