0

I have a method that will copy an Audio file from raw folder to SD card. It takes two inputs

  • ressound: .ogg Audio Raw file id.
  • fName: the file name of the raw file in the SD card.

Updated

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

If the method returns true, Then the file is saved correctly to SD card. If it returns false, then there is an error. In my case, The method returns false when it enters the FileNotFoundException block. I can't find why !! everything seems fine to me.

In case you ask me if I add the WRITE permission, I added it to the application tag in the Manifest file:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Any help please ?

iTurki
  • 16,292
  • 20
  • 87
  • 132
  • Consider using `File f = Environment.getExternalStorageDirectory();` for getting SD Card directory in device independed way. – Im0rtality Aug 08 '11 at 08:45

3 Answers3

2

Use this for path:

string baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
string path = baseDir + File.Separator + "music" + File.Separator + "yourpackagename" + filename;
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
0

Please don't hardcode paths. Use Environment.getExternalStorageDirectory() to get the path of a large storage medium. Some phones mount sdcards in /mnt/sdcard or somewhere else, some just don't have one and rely on inbuilt flash.

This might throw the error, but I don't think is the error in this case since you are probably testing with your phone/emulator, where this path applies.

0

My problem has nothing to do with the code. It is a permission-related thing. I added the permission inside the application tag. It must be outside it. It must be in the root tag.

Wired for me because it make sense to put it in the application tag not the Manifest tag !

Thanks for your answers !

iTurki
  • 16,292
  • 20
  • 87
  • 132