Download MP3 and put it in sdcard

Source: Internet
Author: User

First, install the virtual sdcard in the Virtual Machine and match it. Then, set the read and write permissions on the storage card in manifest. xml.

Mainactivity code

1 package COM. example. downloadtext; 2 3 Import android. app. activity; 4 Import android. OS. bundle; 5 import android. OS. environment; 6 Import android. util. log; 7 Import android. view. view; 8 Import android. widget. button; 9 Import android. widget. toast; 10 11 import Java. io. file; 12 Import Java. io. ioexception; 13 Import java.net. httpurlconnection; 14 15 public class myactivity extends activity16 {17/** 18 * called when the activity is first created.19 */20 static string sdcardpath = "/storage/sdcard /"; 21 button download; 22 23 @ override24 public void oncreate (bundle savedinstancestate) 25 {26 super. oncreate (savedinstancestate); 27 setcontentview (R. layout. main); 28 download = (button) findviewbyid (R. id. download); 29 download. setonclicklistener (New downloadlistener (); 30 31 32} 33 34 35 class downloadlistener implements view. onclicklistener36 {37 @ override38 39 public void onclick (view) 40 {log. V ("button", "button has been clicked"); 41 42 new downloadthread (). start (); 43 file = new file (sdcardpath + "song"); 44 If (file. exists () 45 {46 toast. maketext (myactivity. this, "Download sucess", toast. length_long); // toast can only be displayed in the UI process, so these cannot be displayed 47 48} 49 else50 {51 toast. maketext (myactivity. this, "Download failed", toast. length_long); 52 53} 54 55} 56 57 58} 59 60 class downloadthread extends thread61 {62 @ override63 public void run () {64 log. V ("Thread", "New thread is started"); 65 download downloadmp3 = new download (); 66 try67 {68 downloadmp3.downloadmp3 ("http:// SC .111ttt.com/up/mp3/276964/2684CCF02022F454B9E67B5D35F9DDF0.mp3 "); 69} 70 catch (ioexception e) 71 {72 E. printstacktrace (); 73} 74 log. V ("Thread", "thread is overed"); 75} 76} 77}
Download class
package com.example.downloadtext;import java.io.*;import java.net.HttpURLConnection;import java.net.URL;import java.nio.Buffer;import java.nio.ByteBuffer;/** * Created by Administrator on 2014/8/14. */public class Download {    public void downloadmp3(String urlpath)throws IOException    {        URL url = new URL(urlpath);        HttpURLConnection urlconnection = (HttpURLConnection) url.openConnection();        InputStream inputStream = urlconnection.getInputStream();        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));        FileOutputStream fos = new FileOutputStream(new File(MyActivity.sdcardpath+"song"));        String readed;        while((readed =reader.readLine() )!=null)        {            fos.write(readed.getBytes());        }        fos.close();        inputStream.close();     } }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.