【问题标题】:boolean issue on copy to SD, need some help to correct复制到 SD 的布尔问题,需要一些帮助来纠正
【发布时间】:2013-09-01 21:22:49
【问题描述】:

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

boolean saved = saveas(sound.getSoundResourceId(),"filename");
因为没有使用变量
saved
。 我不太确定如何解决这个问题,或者我应该用什么代替saved,只是想知道这里是否有人有线索? 这是完整的代码:
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);
    }
}}

【问题讨论】:

  • 不清楚您遇到了什么问题

标签: java android boolean contextmenu


【解决方案1】:

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

【讨论】:

  • 谢谢!这摆脱了错误,但仍然没有复制到 SD 卡。只是想知道我的路径是否井井有条,因为我知道在源代码中硬编码路径并不好。你能看到任何其他问题或我可能遗漏的任何东西吗?
  • @SeanBurrage:好吧,您的ACTION_MEDIA_SCANNER_SCAN_FILE 似乎是错误的。保留new File(completePath)(而不是多次创建),然后使用Uri.fromFile(completePath) 作为您的Intent
  • 您是否有机会发布代码的内嵌编辑?你似乎是这里唯一能提供帮助的人。
  • @SeanBurrage:说白了,如果你不能实现我所描述的,你需要离开Android开发一段时间,学习Java。试图同时学习 Java 和 Android 可能会让您和其他人感到沮丧。话虽如此,请将sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename))); 替换为sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(completePath)));(不完全是我的建议,但它应该可以解决问题)。
  • 不不抱歉,我漏掉了一个词,只是每次我尝试你的建议时都会出现错误,但现在很好。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-21
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多