gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:49:37 25 4
gpt4 key购买 nike

我遇到问题,在尝试将音频文件从 raw 文件夹复制到 SD 卡时,我已成功在 SD 卡中创建文件夹,但无法复制其中的歌曲...

我正在使用本教程: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();
}

日志说:

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

最佳答案

用我的替换你的代码。

它在这里运行完美。

仅添加此权限:

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

Java 代码:

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 - 如何将原始文件复制到 SD 卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19464162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com