简体   繁体   English

安卓 正确下载mp3

[英]Android. To download mp3 properly

I faced with a weird problem. 我面临一个奇怪的问题。 I write android application for downloading mp3 files. 我编写用于下载mp3文件的android应用程序。 I download mp3 with url, it works fine. 我用网址下载mp3,效果很好。 But any player except VLC can not find these downloaded mp3 files on device. 但是,除了VLC之外,任何播放器都无法在设备上找到这些下载的mp3文件。 And if I use any file manager I can find these files but they dont have mp3 tags. 而且,如果我使用任何文件管理器,我都可以找到这些文件,但它们没有mp3标签。

For example. 例如。

This is a file downloaded with my application. 这是与我的应用程序一起下载的文件。 I open its properties with FX file manager. 我使用FX文件管理器打开其属性。

在此处输入图片说明

And it is mp3 downloaded with another program (not mine). 而且它是通过另一个程序(不是我的)下载的mp3。 As you can see the file has mp3 tags (shown in bottom of screen) 如您所见,该文件具有mp3标签(显示在屏幕底部)

在此处输入图片说明

It is my code for downloading files: 这是我下载文件的代码:

@Override
protected Void doInBackground(String... params) {               
    InputStream inputStream =  null;
    FileOutputStream fileOutput =  null;
    try {
        URL url = new URL(params[0]); 
        File file = new File(path);              
        URLConnection urlConnection = url.openConnection();

        inputStream = urlConnection.getInputStream();
        fileOutput = new FileOutputStream(file);

        int totalSize = urlConnection.getContentLength();
        int downloadedSize = 0;

        byte[] buffer = new byte[16384];
        int bufferLength = 0; 

        while ((bufferLength = inputStream.read(buffer)) > 0 ) {

            while(isPaused) {
                sleep();
            }

            if(isCancelled()) {                 
                if(file.exists())
                    file.delete();
                return null;
            }

            fileOutput.write(buffer, 0, bufferLength);
            downloadedSize += bufferLength;
            publishProgress(downloadedSize, totalSize);
        }

        if(totalSize > getFreeMemorySize()) {
        //if(true) {
            if(errorHandler != null)
                errorHandler.onMemorySizeException();
            cancel(true);
        }                       
    } catch (IOException e) {
        int i = e.hashCode();
        e.getStackTrace();
    }
    finally {
        try {

            if(inputStream != null)
                inputStream.close();
            if(fileOutput != null)
                fileOutput.close();    

        } catch (IOException e) {
            int i = e.hashCode();
            e.getStackTrace();
        }
    }
    return null;
} 

Where I am wrong? 我哪里错了? Why mp3-files downloaded with my application can npt be found with mp3-players? 为什么在我的应用程序中下载的mp3文件可以在mp3播放器中找到npt? How could I fix it? 我该如何解决?

What you are observing is the fact that MediaStore does not constantly update his database. 您正在观察的事实是MediaStore不会不断更新其数据库。 Database is updated only after reboot and after sd card is mounted. 仅在重启后和安装sd卡后才更新数据库。

If you want your files to appear immediately you have to tell MediaStore to add them. 如果要立即显示文件,则必须告诉MediaStore添加它们。

Use MediaScannerConnection.scanFile method to notify MediaStore. 使用MediaScannerConnection.scanFile方法通知MediaStore。 ( MediaScannerConnection doucumentation ) Note that you can add multiple files/paths at once. MediaScannerConnection双重记录 )请注意,您可以一次添加多个文件/路径。 Also worth noting is that this method is asynchronous, files are added in a separate process and this can take some time - you are notified when operation is completed. 另外值得注意的是,该方法是异步的,文件是在单独的过程中添加的,这可能需要一些时间-操作完成时会通知您。

MediaScannerConnection.scanFile(
  context,
  new String[]{file.getAbsolutePath()},
  null,
  new OnScanCompletedListener() {
     @Override
     public void onScanCompleted(String path, Uri uri) {
         // only at this point are files in MediaStore
     }
  });

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

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