Android-通过URL命名问题将mp3下载到SDCARD
我目前有一个带有各种 mp3 链接的 webview
。如果用户按下这些链接之一,将弹出一个 alertDialog
,他们可以选择是否收听或下载该文件。虽然我的下载部分按预期工作(通过asynctask
),但我当前已将其设置为指定 SDCARD 上的 mp3 文件将被调用的名称。我希望曲目的名称就是 mp3 文件的名称。关于我如何做到这一点有什么想法吗?谢谢。
这是我的代码的一部分:
//so you can click on links in app and not open the actual browser. will stay in app
private class HelloWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url){
view.loadUrl(url);
view.getSettings().getAllowFileAccess();
view.getSettings().setJavaScriptEnabled(true);
//load the dropbox files so people can listen to the track
if(url.endsWith(".mp3")){
progressWebView.dismiss();
progressWebView.cancel();
blogDialog.setButton("Listen", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "audio/*");
view.getContext().startActivity(intent);
}
});
blogDialog.setButton2("Download", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sdrUrl = url.toString();
new DownloadFile().execute();
}
});
blogDialog.show();
}else{
return super.shouldOverrideUrlLoading(view, url);
}
return true;
}
}
//to handle the back button
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if((keyCode == KeyEvent.KEYCODE_BACK) && sdrWebView.canGoBack()){
sdrWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onPause(){
super.onPause();
}
/*create the pop up menu so you can reload*/
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.refreshsetting: sdrWebView.loadUrl("http://www.stopdroprave.com");
break;
}
return true;
}
private class DownloadFile extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... url) {
try {
URL url2 = new URL(sdrUrl);
HttpURLConnection c = (HttpURLConnection) url2.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lengthOfFile = c.getContentLength();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.v("", "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
String fileName = "testSDRtrack.mp3";
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
publishProgress((int)(len1*100/lengthOfFile));
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
}
}
I currently have a webview
with various mp3 links. If the user presses on one of these links, an alertDialog
will pop up and they can choose whether to listen or download the file. While my download portion works intended (via an asynctask
), I have it currently set up where I specify the name which the mp3 file on the SDCARD will be called. I would like to have it so that the name of the track is the name of the mp3 file. Any ideas on how I could do that? Thanks.
Here is portion of my code:
//so you can click on links in app and not open the actual browser. will stay in app
private class HelloWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url){
view.loadUrl(url);
view.getSettings().getAllowFileAccess();
view.getSettings().setJavaScriptEnabled(true);
//load the dropbox files so people can listen to the track
if(url.endsWith(".mp3")){
progressWebView.dismiss();
progressWebView.cancel();
blogDialog.setButton("Listen", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "audio/*");
view.getContext().startActivity(intent);
}
});
blogDialog.setButton2("Download", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sdrUrl = url.toString();
new DownloadFile().execute();
}
});
blogDialog.show();
}else{
return super.shouldOverrideUrlLoading(view, url);
}
return true;
}
}
//to handle the back button
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if((keyCode == KeyEvent.KEYCODE_BACK) && sdrWebView.canGoBack()){
sdrWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onPause(){
super.onPause();
}
/*create the pop up menu so you can reload*/
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.refreshsetting: sdrWebView.loadUrl("http://www.stopdroprave.com");
break;
}
return true;
}
private class DownloadFile extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... url) {
try {
URL url2 = new URL(sdrUrl);
HttpURLConnection c = (HttpURLConnection) url2.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lengthOfFile = c.getContentLength();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.v("", "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
String fileName = "testSDRtrack.mp3";
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
publishProgress((int)(len1*100/lengthOfFile));
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我基本上分割了 url 并得到了我想要的部分,然后像
Android- split URL string
I essentially split the url and got the portion that I wanted then saved it like that
Android- split URL string