当前位置: 首页 > 知识库问答 >
问题:

如何将原始文件复制到SD卡中

爱刚捷
2023-03-14
 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();
    }

日志说:

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

共有1个答案

吕俊哲
2023-03-14

用我的代码替换你的代码。

它在这里工作得很好。

仅添加此权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
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();
    }
}

}
 类似资料:
  • 本文向大家介绍Android复制assets文件到SD卡,包括了Android复制assets文件到SD卡的使用技巧和注意事项,需要的朋友参考一下 前言 最近接到一个js文件缓存任务,即通过拦截我们webView的url,首先从文件加载js文件,文件里没有的话就去assets里面Copy过来。感觉这个工具类挺有用的,所以先发上来供大家参考。稍后有时间会把整个项目思路写出来。 项目代码 参数说明 项

  • 问题内容: 我希望能够从Android设备的内部缓存中移动或复制文件,并将其放入SD卡上的永久存储区。这是我到目前为止所拥有的: 问题答案:

  • 本文向大家介绍Android实现复制Assets文件到SD卡,包括了Android实现复制Assets文件到SD卡的使用技巧和注意事项,需要的朋友参考一下 Assets文件介绍 assets文件夹里面的文件都是保持原始的文件格式,需要用AssetManager以字节流的形式读取文件。 1. 先在Activity里面调用getAssets() 来获取AssetManager引用。 2. 再用Asse

  • 问题内容: 我的Android手机上有一个数据库,我需要将信息保存到 SD卡上。 是否可以将数据库文件以可读状态保存到SD卡上? 我尚未找到有关如何执行此操作的任何信息。 一些将数据库文件复制到SD卡的源代码将是理想的。 问题答案: 数据库文件与任何其他文件一样,如果您复制二进制文件,它将起作用。 Java没有内置的文件复制方法,因此您可以使用以下方法: 在Java中复制文件的标准简洁方法? 只是

  • 目前,我的(使用的是Stackoverflow,我自己没有完整地编写)代码如下所示: 我得到错误:“打开失败;EACCES(权限被拒绝)”。 我的舱单是这样的:

  • 我知道很多问题已经被回答了,但是在我的例子中,我的代码在Oppo、三星手机上运行正常,但是在MI、MOto G、Lenavo手机上不能运行 下面是我的代码: