【发布时间】:2019-03-05 06:28:54
【问题描述】:
我的应用程序中有一个下载按钮。当我按下下载按钮时,它会从提供的 url 下载音频文件。但问题是我想将文件保存在名为“DownloadTestFolder”的内部存储文件夹中,它显示一个错误。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download"
android:onClick="Download"
android:id="@+id/download"/>
</LinearLayout>
public class DownloadTest extends Activity {
DownloadManager downloadManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.download_test);
}
public void Download(View view){
downloadManager=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri=Uri.parse("My Url");
DownloadManager.Request request = new DownloadManager.Request(uri);
try{
request.setDestinationInExternalPublicDir(getFilesDir()+"/DownloadTestFolder","/");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Long reference = downloadManager.enqueue(request);
}catch (Exception e){
/* Error
}
}
}
【问题讨论】:
-
检查这个它的好库 - github.com/lingochamp/FileDownloader
-
是否有必要标记“javascript”?