首页 > 解决方案 > 移动文件的代码不起作用

问题描述

在我的 android 项目中,我想移动一个文件,但它崩溃了。我使用了以下代码:

copyFile(getResources().openRawResource(R.raw.world_resource_packs), "/storage/emulated/0/games/com.mojang/minecraftWorlds/Escape The Room (Original by Twister Games)/world_resource_packs.json");

public void copyFile(InputStream inputStream, String outputPath){

    System.out.println("1");
    in = inputStream;
    out = null;
    System.out.println("2");
    try {
        out = new FileOutputStream(outputPath);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    System.out.println("3");
    byte[] buff = new byte[1024];
    int read = 0;
    System.out.println("4");

    try {
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }
        System.out.println("5");
        System.out.println("6");
        System.out.println("7");
        System.out.println("8");
        System.out.println("9");
        System.out.println("10");
    } catch (IOException e) {
        e.printStackTrace();
    }

}

我也把它放在我的清单中:

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

但它仍然崩溃:

E/AndroidRuntime:致命异常:主进程:twistergames.de.testverschieben,PID:11228 java.lang.RuntimeException:无法启动活动 ComponentInfo{twistergames.de.testverschieben/twistergames.de.testverschieben.MainActivity}:java.lang。 NullPointerException:尝试在 android.app 的 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793) 的空对象引用上调用虚拟方法 'void java.io.FileOutputStream.write(byte[], int, int)' .ActivityThread.handleLaunchActivity(ActivityThread.java:2864) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 在 android.os.Handler.dispatchMessage (Handler.java:105) 在 android.os.Looper.loop(Looper.java:156) 在 android.app.ActivityThread.main(ActivityThread.java:6523) 在 java.lang。reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)作者:java.lang.NullPointerException:尝试在 twistergames.de.testverschieben.MainActivity.copyFile(MainActivity. java:44) attwistergames.de.testverschieben.MainActivity.onCreate(MainActivity.java:23) at android.app.Activity.performCreate(Activity.java:6915) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123 ) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 在 android.os.Handler.dispatchMessage(Handler.java:105) 在 android.os.Looper.loop(Looper.java:156) 在android.app.ActivityThread.main(ActivityThread.java:6523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)主要(ZygoteInit.java:832)主要(ZygoteInit.java:832)

非常感谢!!!

标签: androidfilemove

解决方案


这是实际的错误描述:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'void java.io.FileOutputStream.write(byte[], int, int)'

所以你在尝试打开输出流时遇到了 IOException

try {
        out = new FileOutputStream(outputPath);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

所以在指定的路径上没有文件。

另外我建议不要忽略 FileNotFoundException 并退出方法或重新抛出 RuntimeException。

确保路径为“/storage/emulated/0/games/com.mojang/minecraftWorlds/Escape The Room (Original by Twister Games)/world_resource_packs.json”的文件存在并在需要时创建它


推荐阅读