Android文件复制导致java.io.IOException: file descriptor closed



我的应用程序复制了一堆文件,请求以列表的形式出现。我有一个ContentProvider实现,所以我需要复制一个文件,并注册它与我的文件提供程序。我尝试了两件事-为每个文件副本剥离一个AsyncTask,并剥离一个AsyncTask来处理所有这些文件。我面临的问题是,与多个AsyncTasks手机真的减慢了一秒钟或两秒钟。然而,当一个一个地处理文件时,我得到IOException -文件描述符关闭:

                D  .doInBackground() - outputUri is /photo/20150602004304409
                D  .doInBackground() - outputUri is /photo/20150602004304421
                D  .doInBackground() - outputUri is /photo/20150602004304423
                D  .doInBackground() - outputUri is /photo/20150602004304426
                D  .doInBackground() - outputUri is /photo/20150602004304428
                E  java.io.IOException: File descriptor closed
                E      at libcore.io.Posix.writeBytes(Native Method)
                E      at libcore.io.Posix.write(Posix.java:223)
                E      at libcore.io.BlockGuardOs.write(BlockGuardOs.java:313)
                E      at libcore.io.IoBridge.write(IoBridge.java:497)
                E      at java.io.FileOutputStream.write(FileOutputStream.java:186)

这是我的代码:

 Uri outputUri = Uri.parse(String.format(path, context.getPackageName()));
 mOutputUri = context.getContentResolver().insert(outputUri, null);
 if (AndroidHelper.isGingerBreadMR1()) {
     execute();
 } else {
     executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
 }

& lt;…>

        InputStream inputStream = null;
        OutputStream outputStream = null;
        boolean error = false;
        Log.d(TAG,".doInBackground() - outputUri is "+mOutputUri.getPath());
        try {
            inputStream = mContext.getContentResolver().openInputStream(mInputUri);
            FileDescriptor fileDescriptor = getMediaFileDescriptor(mOutputUri);
            if (fileDescriptor != null) {
                outputStream = new FileOutputStream(fileDescriptor);
                byte[] buffer = new byte[1024];
                int read;
                while ((read = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, read);
                }
            }
        } catch (IOException e) {
            Log.e(TAG, ".doInBackground - exception caught while copying the file",e);
            error = true;
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                Log.e(TAG, ".doInBackground - exception caught while closing input stream",e);
                error = true;
            }
        }
        return (error) ? mInputUri : mOutputUri;

getMediaFileDescriptor看起来像这样:

private FileDescriptor getMediaFileDescriptor(Uri outputUri) {
    try {
        return mContext.getContentResolver().openFileDescriptor(outputUri, "w")
                .getFileDescriptor();
    } catch (FileNotFoundException e) {
        Log.e(TAG, ".getMediaFileDescriptor() - Error opening media output stream", e);
    }
    return null;
}

我有一些问题,它不是很好,但你必须在关闭它之前冲洗流。

while ((read = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, read);
                outputStream.Flush();
            }

我的问题解决了这个代码

您还应该确保在完成getContentResolver().openFileDescriptor返回的ParcelFileDescriptor时调用.close()

相关内容

  • 没有找到相关文章

最新更新