3

Well, I tried a lot of things, but the better goal that I achieve is copy the desired file to sd, but the new file size is 0bytes always :(

This is my code :

        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        String fileName = sonidoActual+".mp3";

        File newSoundFile = new File(baseDir, fileName);
        Uri mUri = Uri.parse("android.resource://com.genaut.ringtonelists/raw/"+sonidoActual);
        AssetFileDescriptor soundFile;
        try {
            soundFile= getContentResolver().openAssetFileDescriptor(mUri, "r");
        } catch (FileNotFoundException e) {
            soundFile=null;   
        }

        try {
            byte[] readData = new byte[1024*500];
            FileInputStream fis = soundFile.createInputStream();
            FileOutputStream fos = new FileOutputStream(newSoundFile);
            int i = fis.read(readData);
            while (i != -1) {
                fos.write(readData, 0, i);
                i = fis.read(readData);
                Log.d("Degug - While: ", "i: "+i);
            }
            fos.flush();
            fos.close();
        } catch (IOException io) {
        }

Sorry for my bad english.Any one know the problem? Thanks a lot!

I have these permissions on my manifest:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

More info: I tried some code to copy files from assets and not work.. I have some app that set Ringtone well, so I don't think that my SD has a problem.. I feel hopeless :S

These are the code from assets, apparently works fine: https://stackoverflow.com/a/4530294/1422434


I tried too with getResources.openRawResource(): but not works

    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = nombreActual+".mp3";

File newSoundFile = new File(baseDir, fileName);

try {
    byte[] readData = new byte[1024*500];
    InputStream fis = getResources().openRawResource(contexto.getResources().getIdentifier(sonidoActual,"raw", contexto.getPackageName()));
    FileOutputStream fos = new FileOutputStream(newSoundFile);
    int i = fis.read(readData);

    while (i != -1) {
        fos.write(readData, 0, i);
        i = fis.read(readData);
    }

    fos.close();
} catch (IOException io) {
}
Community
  • 1
  • 1
Genaut
  • 1,810
  • 2
  • 29
  • 60

1 Answers1

2

As you are using the raw folder, simply use this from your activity:

getResources().openRawResource(resourceName)
bunbun
  • 2,595
  • 3
  • 34
  • 52
  • If I change my FileInputStream for a InputStream with a openRawResource, not work yet – Genaut Aug 05 '12 at 15:59
  • Wow, I tried again with inputStream instead of FileInputStream and works! Thanks a lot!! :) You make me happy!! – Genaut Aug 05 '12 at 16:27