动态保存或相对于按下的按钮保存到SD卡和上下文菜单功能

问题描述 投票:1回答:1

相对于下面的代码,我该怎么做

if (savering(R.raw.sound1)){

String filename=”sound1″+”.ogg”;

values.put(MediaStore.MediaColumns.TITLE, “sound1″);

动态而不是静态,以便将要保存的声音文件基于所按下的按钮?

我的声音文件位于保存为sound1,sound2,sound3等的soundArray中。每个声音文件都有一个按钮。点击播放,长按将弹出以下上下文菜单。

任何帮助或提示将不胜感激。

//CONTEXT MENU

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
     menu.setHeaderTitle("Save as...");
     menu.add(0, v.getId(), 0, "Ringtone");
     menu.add(0, v.getId(), 0, "Notification");
    }
    @Override   
    public boolean onContextItemSelected(MenuItem item) { 
     if(item.getTitle()=="Ringtone"){function1(item.getItemId());}   
      else if(item.getTitle()=="Notification"){function2(item.getItemId());}  
      else {return false;}
     return true; 
    }

    public void function1(int id){  
     if 
     (savering(R.raw.sound1)){   
      // Code if successful   
      Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show(); 
     }           
     else           
     { 
      // Code if unsuccessful   
      Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
     }

    }
    public void function2(int id){   
     if 
     (savenot(R.raw.sound1)){   
      // Code if successful   
      Toast.makeText(this, "Saved as Notification", Toast.LENGTH_SHORT).show(); 
     }           
     else           
     { 
      // Code if unsuccessful   
      Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show(); 
     }
    }

//Save into Ring tone Folder

    public boolean savering(int ressound){
     byte[] buffer=null;
     InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
     int size=0; 

     try {
       size = fIn.available();   
       buffer = new byte[size];   
       fIn.read(buffer);   
       fIn.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block   
      return false;      } 

     String path="/sdcard/media/audio/ringtones/";
     String filename="sound1"+".ogg"; 

     boolean exists = (new File(path)).exists();   
     if (!exists){new File(path).mkdirs();}   

     FileOutputStream save;
     try { 
      save = new FileOutputStream(path+filename);   
      save.write(buffer);   
      save.flush();   
      save.close();   
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block   
      return false;  
     } catch (IOException e) {
      // TODO Auto-generated catch block   
      return false;
     }
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename))); 

     File k = new File(path, filename);   
     ContentValues values = new ContentValues();   
     values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());   
     values.put(MediaStore.MediaColumns.TITLE, "sound1 Ringtone");   
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");   
     values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");   
     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, false);    

     //Insert it into the database
     this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);

     return true; 
    }

//Save in Notification Folder

    public boolean savenot(int ressound){
     byte[] buffer=null;
     InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
     int size=0; 

     try {
       size = fIn.available();   
       buffer = new byte[size];   
       fIn.read(buffer);   
       fIn.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block   
      return false;      } 

     String path="/sdcard/media/audio/notifications/";
     String filename="sound1"+".ogg"; 

     boolean exists = (new File(path)).exists();   
     if (!exists){new File(path).mkdirs();}   

     FileOutputStream save;
     try { 
      save = new FileOutputStream(path+filename);   
      save.write(buffer);   
      save.flush();   
      save.close();   
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block   
      return false;  
     } catch (IOException e) {
      // TODO Auto-generated catch block   
      return false;
     }
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename))); 

     File k = new File(path, filename);   
     ContentValues values = new ContentValues();   
     values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());   
     values.put(MediaStore.MediaColumns.TITLE, "sound1 Notification");   
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");   
     values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");   
     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, false);    

     //Insert it into the database
     this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);

     return true; 
    }
java android eclipse android-sdcard android-context
1个回答
1
投票

我认为您可以通过以下更改获得解决方案:

1:取两个数组。一个用于存储您要添加到菜单中播放的ringbuttons id,另一个用于分别存储该按钮的声音的相应声音array id的数组;

例如,

int btnid=new int[count];//count is no of buttons for playing ring
int rawid=ne int[count];

然后,您可以捕获单击的菜单项的按钮的ID,并从中必须在function1中与btnid数组进行比较,并获得匹配的btnid数组的索引。

2:使用相同索引之后,您可以在rawid数组中找到rawsource。


您的第一个问题已解决,现在对于文件名的第二个问题,您只需尝试以下操作:

String filename="sound"+id+".ogg";//id is index of array 
© www.soinside.com 2019 - 2024. All rights reserved.