1

I'm (slowly) making an app which displays a list of tones and lets the user "long press" a certain one which then brings up a context menu asking if you'd like to copy it to SD card. Only problem is that last part, I need help. Basically the tones are stored in the Raw folder, and I need it that it copies the selected tone file to the SD card, preferably in the notifications folder.

Just wondering if someone could give me an example of how I would go about this because I'm absolutely lost?

Here's my code

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);
    }
}}

3 Answers3

2

Refer the below links. It will help you..

Copying raw file into SDCard?

Move Raw file to SD card in Android

Copy file from raw dir to SDcard

Community
  • 1
  • 1
Kavin
  • 544
  • 3
  • 5
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;  
    } 

try this instead of the one you have

@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);
    }
}}
JRowan
  • 6,824
  • 8
  • 40
  • 59
  • Can you give me an example on where I would start modifying this to my needs? All my "Sounds Clips" are listed in the main thread and not in a string. – Sean Burrage Aug 25 '13 at 19:35
  • Would you be willing to help if I paid you? – Sean Burrage Aug 25 '13 at 19:41
  • 1
    im not for hire, i have a bunch of projects im working on myself, this is a method that you use, you put in the R.raw.id, and the string name of what you want to save the file as, you can change the directory and file type with these lines String path="/sdcard/music/[my_package_name]/"; String filename=fName+".ogg"; – JRowan Aug 25 '13 at 23:37
  • How would I get the raw.id from a selection on the context menu? – Sean Burrage Aug 26 '13 at 09:06
  • with a public int keep track of the position for onListItemClick, then in your context menu you will have the position, pull up the sound description and if it matches the string you know witch id to use – JRowan Aug 26 '13 at 09:54
  • Ok I'm lost, think I'm in over my head. think I might just give up on this project :-( – Sean Burrage Aug 26 '13 at 10:38
  • there you go, i updated, try it like that, let me know if it works – JRowan Aug 27 '13 at 03:42
  • Four lines from bottom "boolean saved":value of local variable not used. – Sean Burrage Aug 27 '13 at 09:04
  • @SeanBurrage `value of local variable not used` is not an error, but a warning from your IDE. Your method `saveas(..., ...)` return a `boolean` and the value is stored in `saved` variable. Right after you assigned `boolean saved = saveas()`, you `return true`. This is what caused the warning, the value of `saved` is not used. – Aprian Aug 28 '13 at 09:03
0

Try this. Call it by:

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();
}
}

Modify this to suit your needs.

Also you need the permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • I'm in way over my head here :-( – Sean Burrage Aug 25 '13 at 19:41
  • 1
    You have to start somewhere... Two months ago I was clueless about Java language and Android was out of this world... I just asked, asked and asked and researched, researched and researched and now I can find my way around, know most of Java and fully understand the structure of Android. Few hours a day is more than enough. Try this: 1. What is it you're trying to achieve? For example... Writing files stored in your app to the SD card. 2. Have you Googled this? Try "Android write raw files to sdcard tutorial" or "Android write raw files to sd card stackoverflow" 3. Do Tutorials they're best –  Aug 26 '13 at 11:43
  • I think it's just I've done everything the wrong way, like my array list which I know shouldnt be in the main thread, but it was the only tutorial I could find online that was easy-ish to understand. Unfortunately I live in a very remote place in Scotland and trying to find someone to help is near impossible. – Sean Burrage Aug 26 '13 at 17:24