2013-01-25 127 views
-1

我有應用程序播放從互聯網上的一些mp3文件 ,我想添加「下載按鈕」從互聯網上下載任何mp3文件 我想從瀏覽器下載,,,而不是從應用,,,但如果我要撥打的瀏覽器,如:下載MP3文件


startActivity(新意圖(Intent.ACTION_VIEW,Uri.parse( 「xxxx.xxxx.xxxx.mp3」)));


瀏覽器要玩這款MP3文件:(

我想我可以鏈接後添加的東西直接下載,,,,任何一個能幫助我

注:如果你有一個代碼,從應用程序下載,,,告訴我關於它也:) 我是新的android世界:P和我想要一個簡單的代碼,以瞭解它 謝謝大家:)

回答

5

你會過得更好使用DownloadManager。 您可以通過拉爾斯·沃格爾here

看到示例代碼的相關位:

DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
     Request request = new Request(
       Uri.parse("http://www.vogella.de/img/lars/LarsVogelArticle7.png")); 
     enqueue = dm.enqueue(request); 

注意,下載管理器可以用API版本9(的Android 2.3薑餅)和向上。

+0

當然,更好的是DownloadManager :) 但我想問你我怎麼能確定SD上的路徑嗎?! –

+0

使用Request對象的setDestinationInExternalPublicDir方法。例如,要將mp3文件保存爲默認的MUSIC目錄中的「song.mp3」,請使用request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC,「song.mp3」); – WeNeigh

+0

非常感謝你:*♥ –

0

你可以把這個放在你的download.php文件

<?php 
    $file = $_GET['file']; 
    header ("Content-type: octet/stream"); 
    header ("Content-disposition: attachment; filename=".$file.";"); 
    header("Content-Length: ".filesize($file)); 
    readfile($file); 
    exit; 
    ?> 

然後:

Uri.parse("direct_download.php?file=filename.mp3"); 
+0

vodich我想從YouTube下載一個MP4文件,如http://r4---sn-u2oxu-f5f6.googlevideo.com/videoplayback?..等等blah.mp4。告訴我,如果我把它叫做它將被下載的地方,以及我需要改變多少php代碼。 – Nepster