Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

android - How to Copy Raw files into SD Card

I am having problem, while trying to copy audio files from raw folder to SD Card, I have successfully created folder in SD Card, but not able to copy songs in that...

I am using this tutorial: http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/

 String path = Environment.getExternalStorageDirectory() + "/PriyankaChopra" ;
    File dir = new File(path);
     try{
       if(dir.mkdir()) {
           final int[] mSongs = new int[] { R.raw.exotic, R.raw.pyaar, R.raw.tinka };
            for (int i = 0; i < mSongs.length; i++) {
                try {
                     String str_song_name= i +".mp3";
                     CopyRAWtoSDCard(mSongs[i], "/sdcard/PriyankaChopra/"+str_song_name);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }   
       } else {
          System.out.println("Error, while Copying songs to SD Card!");

       }
     }catch(Exception e){
       e.printStackTrace();
     }

    }   

    private void CopyRAWtoSDCard(int id, String path) throws IOException {
        InputStream in = getResources().openRawResource(id);
        FileOutputStream out = new FileOutputStream(path);
        byte[] buff = new byte[1024];
        int read = 0;

        try {
            while ((read = in.read(buff)) > 0) {
                out.write(buff, 0, read);
            }
        } finally {
            in.close();
            out.close();
    }

Log says:

10-19 04:57:43.729: D/dalvikvm(997): GC_FOR_ALLOC freed 77K, 7% free 2942K/3136K, paused 64ms, total 67ms
10-19 04:57:44.019: I/System.out(997): Error, while Copying songs to SD Card!
10-19 04:57:44.328: D/gralloc_goldfish(997): Emulator without GPU emulation detected.
10-19 05:39:32.988: D/dalvikvm(4086): GC_FOR_ALLOC freed 53K, 6% free 2964K/3136K, paused 45ms, total 47ms
10-19 05:39:33.318: I/System.out(4086): Error, while Copying songs to SD Card!
10-19 05:39:34.209: D/gralloc_goldfish(4086): Emulator without GPU emulation detected.
10-19 06:20:10.529: D/dalvikvm(8485): GC_FOR_ALLOC freed 46K, 6% free 2964K/3132K, paused 37ms, total 39ms
10-19 06:20:11.369: D/AndroidRuntime(8485): Shutting down VM
10-19 06:20:11.369: W/dalvikvm(8485): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-19 06:20:11.500: E/AndroidRuntime(8485): FATAL EXCEPTION: main
10-19 06:20:11.500: E/AndroidRuntime(8485): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.musicplayer/com.androidhive.musicplayer.Music}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
10-19 06:20:11.500: E/AndroidRuntime(8485):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at android.os.Looper.loop(Looper.java:137)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at java.lang.reflect.Method.invokeNative(Native Method)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at java.lang.reflect.Method.invoke(Method.java:525)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at dalvik.system.NativeStart.main(Native Method)
10-19 06:20:11.500: E/AndroidRuntime(8485): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
10-19 06:20:11.500: E/AndroidRuntime(8485):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at java.util.ArrayList.get(ArrayList.java:308)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at com.androidhive.musicplayer.Music.playSong(Music.java:282)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at com.androidhive.musicplayer.Music.onCreate(Music.java:81)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at android.app.Activity.performCreate(Activity.java:5133)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-19 06:20:11.500: E/AndroidRuntime(8485):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-19 06:20:11.500: E/AndroidRuntime(8485):     ... 11 more
10-19 06:22:14.589: D/dalvikvm(8922): GC_FOR_ALLOC freed 50K, 6% free 2964K/3136K, paused 33ms, total 35ms
10-19 06:22:14.809: D/AndroidRuntime(8922): Shutting down VM
10-19 06:22:14.809: W/dalvikvm(8922): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-19 06:22:14.870: E/AndroidRuntime(8922): FATAL EXCEPTION: main
10-19 06:22:14.870: E/AndroidRuntime(8922): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.musicplayer/com.androidhive.musicplayer.Music}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
10-19 06:22:14.870: E/AndroidRuntime(8922):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at android.os.Looper.loop(Looper.java:137)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at java.lang.reflect.Method.invokeNative(Native Method)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at java.lang.reflect.Method.invoke(Method.java:525)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at dalvik.system.NativeStart.main(Native Method)
10-19 06:22:14.870: E/AndroidRuntime(8922): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
10-19 06:22:14.870: E/AndroidRuntime(8922):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at java.util.ArrayList.get(ArrayList.java:308)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at com.androidhive.musicplayer.Music.playSong(Music.java:281)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at com.androidhive.musicplayer.Music.onCreate(Music.java:80)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at android.app.Activity.performCreate(Activity.java:5133)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-19 06:22:14.870: E/AndroidRuntime(8922):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-19 06:22:14.870: E/AndroidRuntime(8922):     ... 11 more
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Replace your code with mine.

it is working perfect here.

Add only this Permission :

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

Java Code :

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;

public class Sample extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final int[] mSongs = new int[] { R.raw.background, R.raw.background1, R.raw.background2 };
    for (int i = 0; i < mSongs.length; i++) {
        try {
            String path = Environment.getExternalStorageDirectory() + "/PriyankaChopra";
            File dir = new File(path);
            if (dir.mkdirs() || dir.isDirectory()) {
                String str_song_name = i + ".mp3";
                CopyRAWtoSDCard(mSongs[i], path + File.separator + str_song_name);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

private void CopyRAWtoSDCard(int id, String path) throws IOException {
    InputStream in = getResources().openRawResource(id);
    FileOutputStream out = new FileOutputStream(path);
    byte[] buff = new byte[1024];
    int read = 0;
    try {
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }
    } finally {
        in.close();
        out.close();
    }
}

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...