I have a download button in my app.When I press the download button it downloads an audio file from provided url.But the problem is I want to save the file in my internal storage folder called "DownloadTestFolder",it is showing an error.
<?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
}
}
}