远程下载音乐为手机铃声核心代码(RingtoneManager与铃声存放路径)

第一步 需判断/sdcard/music/ringtones是否存在文件夹。

       File file = new File(/sdcard/music/ringtones);

      if ( !file.exists() ){

            if (file.mkdirs ){

                  }

       }else{

       }

第二步 取得用户输入的文件路径的文件拓展名与文件名

      String strExt = strURL.substring( strURL.lastIndexOf(" . ")+1,strURL.length()).toLowerCase();

      String strNam = StrURL.substring (strURL.lastIndexOf(" / ")+1,strURL.lastIndexOf(".");

第三步 创建线程在线程中取得远程文件

      Runnable r = new Runnable{

            public void run(){

                 try{

                       getDateSource(strPath);//调用自己定义的取得文件的方法 此方法相当于下载资源

                 }catch(){

                }

           }

     }

    new Thread(r).start();

    public void  getDateSource(String strPath) {

          if( ! URLUtil.isNetworkUrl(strPath)){  //判断路径是否符合网址规则

 

          }else{

                URL  url = new URL(strPath);

                URLConnection conn = url.openConnection();

                conn.connect();

                InputStream in = conn.getInputStream();

                File myfile = new File("/sdcard/music/ringtones" , strExt +" . " +strNam);//创建暂存文件

                String currentFile = myfile.getAbsolutePath();//得到所创建文件的路径

                FileOutPutStream out = new FileOutPutStream( myfile );

                byte b [] = new byte [128] ;

                do{

                    int  inread = in.read(b);

                    if( inread<= 0 ){

                         break;

                     }

                    out.write(b,0,inread);

                 }while(true);

                 //开启RingtoneManager进行铃声选择

                  string uri = null;

                  Intent intent = new  Intent(RingtoneManager.ACTION_RINGTONE_PICKER);//设定铃声

                  /* 设定显示铃声的文件夹 */
                  intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
                   /* 设定显示铃声开头 */
                  intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "设定铃声");
                   if (uri != null)
                     {
                           intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,  Uri.parse(uri));
                     } else
                       {
                            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
                      }
                            startActivityForResult(intent, RINGTONE_PICKED); 

                    in .close();

          }

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
     {
        if (resultCode != RESULT_OK)
       {
         return;
       }
        switch (requestCode)
       {
        case (RINGTONE_PICKED):
          try
            {
              Uri pickedUri = data .getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
             if (pickedUri != null)
             {
            RingtoneManager.setActualDefaultRingtoneUri(EX08_09.this,  RingtoneManager.TYPE_RINGTONE, pickedUri);
             }
            } catch (Exception e)
            {
             e.printStackTrace();
            }
            break;
          default:
           break;
       }

        super.onActivityResult(requestCode, resultCode, data);
    }

你可能感兴趣的:(远程下载音乐为手机铃声核心代码(RingtoneManager与铃声存放路径))