Android:无法写入系统服务以将文件从/ mnt / sdcard复制到/ system / media

时间:2014-02-11 07:00:31

标签: android filenotfoundexception

我只是编写了一个Android系统服务,它可以运行。

/*
 *   framework/base/services/java/com/android/server/MyService.java
 */

package com.android.server;

import android.util.Log;
import android.os.IMyManager;

    class MyService extends IMyManager.Stub
    {

            final String TAG = "MyService";

            public MyService()
            {
                    Log.i(TAG,"MyService is constructed!"); 
            }
    }

在进入启动器应用程序之前,它将在启动时打印“构建MyService”。

但是当我想将文件从/ mnt / sdcard复制到/ system / media时失败了

/*
 *   framework/base/services/java/com/android/server/MyService.java
 */

package com.android.server;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.util.Log;
import android.os.IMyManager;

class MyService extends IMyManager.Stub
{
        final String TAG = "MyService";

        public MyService()
        {
                Log.i(TAG,"MyService is constructed!");

                try {
                    InputStream input = new FileInputStream("/mnt/sdcard/bootanimation.zip");
                    OutputStream output = new FileOutputStream("/system/media/bootanimation.zip");

                    byte data[] = new byte[1024];
                    long total = 0;
                    int count;
                    while ((count = input.read(data)) != -1) {
                        total += count;
                        output.write(data, 0, count);
                    }

                    output.flush();
                    output.close();
                    input.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d(TAG, "Exception in MyService");
                }

                Log.i(TAG,"MyService finishes!");
        }
}

错误消息是FileNotFoundException。 但我确定在/ mnt / sdcard下有一个bootanimation.zip

946:02-11 06:44:54.796   608   608 W System.err: java.io.FileNotFoundException: /mnt/sdcard/bootanimation.zip: open failed: ENOENT (No such file or directory)
947:02-11 06:44:54.796   608   608 W System.err:    at libcore.io.IoBridge.open(IoBridge.java:406)
948:02-11 06:44:54.796   608   608 W System.err:    at java.io.FileInputStream.<init>(FileInputStream.java:78)
949:02-11 06:44:54.796   608   608 W System.err:    at java.io.FileInputStream.<init>(FileInputStream.java:105)
950:02-11 06:44:54.796   608   608 W System.err:    at com.android.server.MyService.<init>(MyService.java:27)
951:02-11 06:44:54.796   608   608 W System.err:    at com.android.server.ServerThread.initAndLoop(SystemServer.java:195)
952:02-11 06:44:54.796   608   608 W System.err:    at com.android.server.SystemServer.main(SystemServer.java:1182)
953:02-11 06:44:54.796   608   608 W System.err:    at java.lang.reflect.Method.invoke(Native Method)
954:02-11 06:44:54.796   608   608 W System.err:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770)
955:02-11 06:44:54.796   608   608 W System.err:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
956:02-11 06:44:54.796   608   608 W System.err: Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
957:02-11 06:44:54.796   608   608 W System.err:    at libcore.io.Posix.open(Native Method)
958:02-11 06:44:54.796   608   608 W System.err:    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:166)
959:02-11 06:44:54.796   608   608 W System.err:    at libcore.io.IoBridge.open(IoBridge.java:392)
960:02-11 06:44:54.796   608   608 W System.err:    ... 8 more

任何人都知道根本原因? 是因为在启动时,/ mnt / sdcard /还没有挂载?

非常感谢。

2 个答案:

答案 0 :(得分:0)

您是否在清单文件中提供了权限?

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

答案 1 :(得分:0)

由于以下拖车线而创建了Exception ...

InputStream input = new FileInputStream("/mnt/sdcard/bootanimation.zip");
OutputStream output = new FileOutputStream("/system/media/bootanimation.zip");

构造函数FileInputStream()FileOutputStream期望File对象作为参数传入。但是你传递的是String值。

因此,要解决此问题,首先,您应该使用这些路径创建File对象,方法是将它们传递给File()构造函数。第二步,将创建的File对象传递给InputStreamOutputStream以读取这些文件。我正在给你更正,应该如下......

//you should not write /mnt/sdcard/ as hardcoded...instead use Environment.getExternalStorageDirectory().getAbsolutePath()

File inputFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "bootanimation.zip");
File outputFile = new File("/system/media/bootanimation.zip");

InputStream input = new FileInputStream(inputFile);
OutputStream output = new FileOutputStream(outputFile);

我想,在此之后你会把一切都搞定。