0

我发布了一个应用程序,我需要将几百 kb 大小的 ogg 文件复制到用户的 sdcard 上。很快我收到了错误报告。就我而言,问题可能是因为该路径不适合该特定手机:

String path = "/sdcard/file.ogg"; 

            File file = new File(Environment.getExternalStorageDirectory(), "file.ogg" );
            if (!file.exists()) {


                InputStream in = getResources().openRawResource(R.raw.file);
                FileOutputStream out = null;
                try {
                    out = new FileOutputStream(path);
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                byte[] buff = new byte[1024];
                int read = 0;

                try {
                    while ((read = in.read(buff)) > 0) {
                        out.write(buff, 0, read);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    try {
                        in.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    try {
                        out.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

所以报告是:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.caodeveloping.androgangnam/com.caodeveloping.androgangnam.Main}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.caodeveloping.androgangnam.Main.onCreate(Main.java:388)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
... 11 more

第 388 行是 out.close(); /sdcard/ 是否可能不是该手机上的合适路径?什么可能导致这种情况?提前致谢!

更新,这个?

String path= Environment.getExternalStorageDirectory().getAbsolutePath();
path += "/file.ogg";
4

1 回答 1

1

两个问题:

a) 显然,您正在尝试关闭文件 (out.close()),如果您无法打开它,则事件

b) 我建议使用 Environment.getExternalStorageDirectory(),而不是硬编码的目录名称“/sdcard”。不保证 SD 卡将安装到此路径。这正是 Environment.getExternalStorageDirectory() API 的原因。

另外,我建议查看整个代码以进行错误处理(如果某些内容无法打开、读取等)

于 2012-11-02T22:14:12.827 回答