2014-02-26 21 views
2

我做Android應用程序,我想,當我在列表視圖中單擊從點擊項目獲得ID,獲取文件,複製該文件,並將其設置爲鈴聲。 但我得到這個錯誤 The method copyFile(AssetManager, String, File) is undefined for the type new AdapterView.OnItemClickListener(){}該方法的CopyFile(AssetManager,字符串,文件)是未定義onItemClick listerner

l.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 

      String selectedName = l.getContext().toString(); 

      AssetManager assetManager = getAssets(); 

      File file = new File(Environment.getExternalStorageDirectory(), 
        "/myRingtonFolder/Audio/"); 
      if (!file.exists()) { 
       file.mkdirs(); 
      } 

      String path = Environment.getExternalStorageDirectory() 
        .getAbsolutePath() + "/FunnysoundsRingtones/Audio/"; 

      File out = new File(path + "/", selectedName);  
      if(!out.exists()){ 
       copyFile(assetManager, "Yeah.mp3", out); 
      }   

      ContentValues values = new ContentValues(); 
      values.put(MediaStore.MediaColumns.DATA, out.getAbsolutePath()); 
      values.put(MediaStore.MediaColumns.TITLE, "RINGTONE"); 
      values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
      values.put(MediaStore.MediaColumns.SIZE, out.length()); 
      values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name); 
      values.put(MediaStore.Audio.Media.IS_RINGTONE, true); 
      values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); 
      values.put(MediaStore.Audio.Media.IS_ALARM, true); 
      values.put(MediaStore.Audio.Media.IS_MUSIC, true); 

      Uri uri = MediaStore.Audio.Media.getContentUriForPath(out.getAbsolutePath()); 
      ContentResolver mCr = getContentResolver(); 
      Uri newUri = mCr.insert(uri, values); 

      try { 
       RingtoneManager.setActualDefaultRingtoneUri(
         getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri); 
       Settings.System.putString(mCr, Settings.System.RINGTONE, 
         newUri.toString()); 
      } 
      catch (Throwable t) 
      { 
       //TODO Handle exception 
      } 

      switch (position) { 
      case 0: 
       if(mp!=null) 
        { 
         mp.release(); 
         mp=null; 
        } 

        mp = MediaPlayer.create(MainActivity.this, R.raw.getchopa); 
        mp.start(); 


       break; 
      case 1: 

我該如何解決這個問題? 有沒有其他方法我應該使用?

回答

3

使用

InputStream in = assetManager.open(filename); 

然後

private void copyFile(InputStream in, OutputStream out) throws IOException { 
     byte[] buffer = new byte[1024]; 
     int read; 
     while((read = in.read(buffer)) != -1){ 
     out.write(buffer, 0, read); 
     } 
} 
+0

+1不錯answer..but忘記關閉流。 –

+0

謝謝!有效! –

相關問題