java - 复制到 SD 的 boolean 问题,需要一些帮助来更正

标签 java android boolean contextmenu

在很多帮助(在这里)的帮助下,我几乎成功地启动并运行了我的应用程序,但我遇到了问题。我基本上是 Java 的初学者,所以这对我来说有点深,但我仍然需要一些帮助。基本上,该应用程序会显示一个音调列表并让您播放它们,如果您触摸并按住它会弹出一个上下文菜单,其中包含一个选项,可以将所选音调复制到 SD 卡。 我知道的问题在于倒数第四行:

boolean saved = saveas(sound.getSoundResourceId(),"filename");
because the variable
saved
is not used. I'm not too sure how to fix this,or what i should put in place of saved, just wondering if anyone on here has a clue? Here's the full code:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerForContextMenu(getListView());
this.getListView().setSelector(R.drawable.selector);
//create a simple list
mSounds = new ArrayList<Sound>();
Sound s = new Sound();
s.setDescription("Affirmative");
s.setSoundResourceId(R.raw.affirmative);
mSounds.add(s);
s = new Sound();
s.setDescription("Anjels");
s.setSoundResourceId(R.raw.anjels);
mSounds.add(s);
s = new Sound();
s.setDescription("Aggro");
s.setSoundResourceId(R.raw.aggro);
mSounds.add(s);
mSounds.add(s);
mAdapter = new SoundAdapter(this, R.layout.list_row, mSounds);
setListAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id){
Sound s = (Sound) mSounds.get(position);
MediaPlayer mp = MediaPlayer.create(this, s.getSoundResourceId());
mp.start();

}@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
  }

public boolean saveas(int ressound, String fName){  
    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="notifications/halon/"; 
    String filename=fName+".ogg";
    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
    String completePath = baseDir + File.separator + "music" + File.separator + "halon" + File.separator + filename;


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

    FileOutputStream save;  
    try {  
     save = new FileOutputStream(completePath);  
     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, "exampletitle");  
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
    values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");  
    values.put(MediaStore.Audio.Media.IS_RINGTONE, false);  
    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;  
   }
@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    Sound sound = (Sound) getListAdapter().getItem(info.position);
    int length = mSounds.size(); // get the length of mSounds object
    String[] names = new String[length]; // creates a fixed array with strings
    for(int i = 0; i < length; i++) {
         // add sound name to string array
         names[i] = mSounds.get(i).getDescription(); // returns the string name
    }
    switch(item.getItemId()) {
    case R.id.copytosd:
          Toast.makeText(this, "Applying " + getResources().getString(R.string.copy) +
                      " for " + names[(int)info.id],
                      Toast.LENGTH_SHORT).show();
          boolean saved = saveas(sound.getSoundResourceId(),"filename");
          return true;
    default:
          return super.onContextItemSelected(item);
    }
}}

最佳答案

如果您不打算使用 saved,请从该语句中删除 boolean saved = 并只使用 saveas(sound.getSoundResourceId(),"filename ");.

关于java - 复制到 SD 的 boolean 问题,需要一些帮助来更正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18511012/

相关文章:

java - Eclipse 终止键盘快捷键

java - 如何在没有重复代码的情况下使用抽屉导航?

android - Google Play -505 安装错误

java - 如何通过按下按钮在android中打开pdf?

vector - 通过字符串输入时的 Clojure boolean 混淆

java - gradle 中的 Ant <resources>、<patternset> 等价于什么?

java - Java 有类似微软的 CHESS 的工具吗?

android - android中的 socks 代理

c - 如果为零则赋值的简写,例如 c = str[i] || 'x'

Java MYSQL,搜索时忽略空白字符串