1

我正在(慢慢地)制作一个显示音调列表并让用户“长按”某个音调的应用程序,然后会弹出一个上下文菜单,询问您是否要将其复制到 SD 卡。唯一的问题是最后一部分,我需要帮助。基本上音调存储在 Raw 文件夹中,我需要将所选音调文件复制到 SD 卡,最好是在通知文件夹中。

只是想知道是否有人可以给我一个例子,说明我将如何解决这个问题,因为我完全迷路了?

这是我的代码

import java.util.ArrayList;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
private ArrayList<Sound> mSounds = null;
private SoundAdapter mAdapter = null;
static MediaPlayer mMediaPlayer = null;
/** Called when the activity is first created. */
@Override
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("Anjels");
s.setSoundResourceId(R.raw.anjels);
mSounds.add(s);
s = new Sound();
s.setDescription("Fizz");
s.setSoundResourceId(R.raw.fizz);
mSounds.add(s);
s = new Sound();
s.setDescription("Flipper");
s.setSoundResourceId(R.raw.flipper);
mSounds.add(s);
s = new Sound();
s.setDescription("Glass Key");
s.setSoundResourceId(R.raw.glasskey);
mSounds.add(s);
s = new Sound();
s.setDescription("Halo");
s.setSoundResourceId(R.raw.halo);
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);
  }
@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    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();
          return true;
    default:
          return super.onContextItemSelected(item);
    }
}}
4

3 回答 3

2

请参阅以下链接。它会帮助你..

将原始文件复制到 SDCard 中?

在 Android 中将原始文件移动到 SD 卡

将文件从原始目录复制到 SD 卡

于 2013-08-24T09:55:34.280 回答
1
 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="/sdcard/music/[my_package_name]/"; 
     String filename=fName+".ogg";
     String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
     String completePath = baseDir + File.separator + "music" + File.separator + "my_package" + 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, 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);  

     //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 = 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);
    }
}}
于 2013-08-24T10:07:19.630 回答
0

尝试这个。通过以下方式调用它:

writeToSD("MyNotes.txt","Some text");

...

public void wrtieToSD(String sFileName, String sBody)
{
try
{
    File root = new File(Environment.getExternalStorageDirectory(), "Notes");
    if (!root.exists()) 
    {
        root.mkdirs();
    }

    File gpxfile = new File(root, sFileName);
    FileWriter writer = new FileWriter(gpxfile);
    writer.append(sBody);
    writer.flush();
    writer.close();

    Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
}
catch(IOException e)
{
     e.printStackTrace();
     importError = e.getMessage();
     iError();
}
}

修改它以满足您的需要。

您还需要许可:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-08-24T09:55:31.463 回答