Java 使用AsyncTask将资产复制到SD卡会引发IOException,但会创建一个0字节的文件

Java 使用AsyncTask将资产复制到SD卡会引发IOException,但会创建一个0字节的文件,java,android,android-asynctask,ioexception,android-assets,Java,Android,Android Asynctask,Ioexception,Android Assets,我使用下面的代码片段将资产目录中的一个项目复制到SD卡上 String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/MyApp/"; String lang = "eng"; String[] paths = new String[] {DATA_PATH, DATA_PATH + "mydata/"}; for (Str

我使用下面的代码片段将资产目录中的一个项目复制到SD卡上

String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/MyApp/";
        String lang = "eng";
        String[] paths = new String[]
                {DATA_PATH, DATA_PATH + "mydata/"};

        for (String path : paths)
        {
            File dir = new File(path);
            if (!dir.exists())
            {
                if (!dir.mkdirs())
                {
                    Log.d(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
                   // return null;
                }
                else
                {
                    Log.d(TAG, "Created Directory " + path + " on sd card");
                }
            }
            Log.d(TAG, "Path " + path + "already exists, not creating");
        }

        if (!(new File(DATA_PATH + "mydata/" + lang + ".dat")).exists())
        {
            try
            {
                Log.d(TAG, "Couldn't find any dat files on SD card, attempting to copy new ones");
                AssetManager assetManager = getAssets();

                String[] filesList = assetManager.list("");
                StringBuilder sb = new StringBuilder();
                for (String s : filesList)
                {
                    Log.d(TAG, "Found file : " + s);
                    if (s.contains("dat"))
                    {
                        try
                        {
                            Log.d(TAG, "Found a dat file, attempting to copy to SD card");
                            InputStream in = assetManager.open("eng.dat");
                            Log.d(TAG, "Opened an inputStream to eng.dat");
                            // GZIPInputStream gin = new GZIPInputStream(in);
                            OutputStream out = new FileOutputStream(DATA_PATH + "mydata/eng.dat");
                            Log.d(TAG, "Opened an outputStream to " + DATA_PATH + "mydata/eng.dat");

                            // Transfer bytes from in to out
                            byte[] buf = new byte[1024];
                            int len;
                            // while ((lenf = gin.read(buff)) > 0) {
                            while ((len = in.read(buf)) != -1)
                            {
                                out.write(buf, 0, len);
                            }
                            in.close();
                            // gin.close();
                            out.close();

                            Log.d(TAG, "Copied " + lang + " .dat");

                        }
                        catch (IOException ioe)
                        {
                            Log.e(TAG, "Was unable to copy " + s + " " + ioe);
                        }
                    }
                    Log.d(TAG, "File : " + s + " is not something we're interested in, not copying to SD card.");
                }
            }
            catch (IOException e)
            {
                Log.e(TAG, "Was unable to copy assets " + e.toString());
            }
        }
我的清单文件中也有这一行:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
似乎在SD卡上成功创建了文件,但始终为0字节。我的卡上有大约700MB的可用空间,我的dat文件只有3MB,所以没有容量问题

我使用的是API级别8

有人能就IOException的原因提出建议吗

谢谢

编辑: 这是堆栈跟踪(感谢Narek提供的提示)


詹姆斯,艾尔西!我读了你的代码,我认为你的问题在这个地方的红色圆圈图上:

byte[] buf = new byte[1024]; 
int len; 
// while ((lenf = gin.read(buff)) > 0) { 
while ((len = in.read(buf)) != -1) { 
    out.write(buf, 0, len); 
    out.flush();//Refresh the buffer output stream.This forced all buffer output byte iswrite to the bottom of the output stream. 
} 
in.close(); 
// gin.close(); 
out.close(); 

你可以试着解决你的问题。我的英语不好,不知道我是否把问题讲清楚了。Flush()方法有关更详细的解释,您可以访问参考API文档。

首先更改line Log.e(标记“无法复制”+s+“”+ioe);到Log.e(标记“无法复制”+s,ioe)@narek.gevorgyan谢谢,我现在拉入跟踪只是为了开玩笑,尝试将文件结尾从“.dat”更改为资产管理器不会尝试压缩为sh*t的内容-例如,将其称为“
eng.mp3
”。Bug很有趣。@Jens你是对的,如果你把它作为一个答案,我会接受,因为它解决了我的问题:)与我的实际代码无关,结果是对资产的文件大小有限制。我添加了一个
.mp3
扩展,如下所示:
05-22 14:28:37.043: E/INITIALISATION(22774): java.io.IOException
05-22 14:28:37.043: E/INITIALISATION(22774):    at android.content.res.AssetManager.readAsset(Native Method)
05-22 14:28:37.043: E/INITIALISATION(22774):    at android.content.res.AssetManager.access$700(AssetManager.java:36)
05-22 14:28:37.043: E/INITIALISATION(22774):    at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
05-22 14:28:37.043: E/INITIALISATION(22774):    at com.example.myapp.task.InitialisationTask.doInBackground(InitialisationTask.java:86)
05-22 14:28:37.043: E/INITIALISATION(22774):    at com.example.myapp.task.InitialisationTask.doInBackground(InitialisationTask.java:19)
05-22 14:28:37.043: E/INITIALISATION(22774):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-22 14:28:37.043: E/INITIALISATION(22774):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-22 14:28:37.043: E/INITIALISATION(22774):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-22 14:28:37.043: E/INITIALISATION(22774):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
05-22 14:28:37.043: E/INITIALISATION(22774):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
05-22 14:28:37.043: E/INITIALISATION(22774):    at java.lang.Thread.run(Thread.java:1096)
byte[] buf = new byte[1024]; 
int len; 
// while ((lenf = gin.read(buff)) > 0) { 
while ((len = in.read(buf)) != -1) { 
    out.write(buf, 0, len); 
    out.flush();//Refresh the buffer output stream.This forced all buffer output byte iswrite to the bottom of the output stream. 
} 
in.close(); 
// gin.close(); 
out.close();