2

我的应用程序复制了一堆文件,请求以列表的形式出现。我实现了一个 ContentProvider,所以我需要复制一个文件并将其注册到我的文件提供程序。我尝试了两件事 - 为每个文件副本分离一个 AsyncTask 并分离一个 AsyncTask 来处理所有这些。我面临的问题是,使用多个 AsyncTask 时,手机的速度确实会变慢一两秒钟。但是,在一个一个处理文件时,我得到了 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);
 }

<...>

        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;
}
4

2 回答 2

0

您还应该确保在完成后调用.close()返回ParcelFileDescriptorgetContentResolver().openFileDescriptor对象。

于 2016-06-02T19:15:10.957 回答
0

我有一些问题,它不好,但你必须在关闭它之前刷新流。

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

这段代码解决了我的问题。

于 2016-01-11T20:22:02.800 回答