将资源文件复制到android上的外部存储不起作用

时间:2014-04-21 17:51:28

标签: android

我正在制作一个媒体播放器应用程序,并试图将一些默认歌曲放到手机上以防用户手机上没有任何内容(我正在通过mediastore提取实际歌曲列表)。

所以我将歌曲放在res / raw文件夹中,并将下面的代码复制起来以便复制它们。它似乎是复制好(因为astro文件浏览器和其他应用程序看得很好)但媒体商店仍然无法找到它们。

我认为这与文件的权限有关,但我不确定是什么。有谁知道为什么?

InputStream song = getResources().openRawResource(R.raw.aquarel);

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "aquarel.mp3");
Log.e(TAG, "File1: " + file.getPath());
OutputStream copySong = new FileOutputStream(file);

byte[] buffer = new byte[1024];
int readvalue = 0;
readvalue = song.read(buffer);

while (readvalue > 0) {
    copySong.write(buffer, 0, readvalue);
    readvalue = song.read(buffer);
}

copySong.close();

1 个答案:

答案 0 :(得分:0)

  

“当您将文件添加到Android的文件系统时,不会选择这些文件   由MedaScanner自动完成。

     

...

     

如果您希望将文件添加到媒体库,则可以执行此操作   通过使用MediaStore内容提供商,或使用   MediaScanner“。

您可以通过发送广播来添加它们:

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);

Source