我的应用程序复制了一堆文件,请求以列表形式出现。我有一个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);
}
< ...>
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;
}
答案 0 :(得分:0)
我有一些问题,但它不好,但你必须在关闭之前冲洗流。
while ((read = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
outputStream.Flush();
}
这个代码解决了我的问题。
答案 1 :(得分:0)
完成后,您还应确保在.close()
返回的ParcelFileDescriptor
上致电getContentResolver().openFileDescriptor
。