언제나 도움이되는 CommonsWare에서 위대한 예제 프로젝트를 확인하십시오. 그것은 당신이 한쪽에 원하는대로의 InputStream과 된 ParcelFileDescriptor 파이프를 만들 수 있습니다, 다른 쪽의 수신 응용 프로그램 :
https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider/Pipe
핵심 부품 openFile
에 파이프를 만드는 :
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
ParcelFileDescriptor[] pipe=null;
try {
pipe=ParcelFileDescriptor.createPipe();
AssetManager assets=getContext().getResources().getAssets();
new TransferThread(assets.open(uri.getLastPathSegment()),
new AutoCloseOutputStream(pipe[1])).start();
}
catch (IOException e) {
Log.e(getClass().getSimpleName(), "Exception opening pipe", e);
throw new FileNotFoundException("Could not open pipe for: "
+ uri.toString());
}
return(pipe[0]);
}
그런 다음 파이프를 가득 채우는 스레드를 만듭니다.
static class TransferThread extends Thread {
InputStream in;
OutputStream out;
TransferThread(InputStream in, OutputStream out) {
this.in = in;
this.out = out;
}
@Override
public void run() {
byte[] buf = new byte[8192];
int len;
try {
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.flush();
out.close();
} catch (IOException e) {
Log.e(getClass().getSimpleName(),
"Exception transferring file", e);
}
}
}
메모리 파일과 메모리 사이에 변환을 포함시킬 계획이 있습니까? 미래에 parcelfiledescriptor? 이 줄을 따라 뭔가가 알려지지 않은 수명을 가진 임시 파일로 파일 시스템을 어수선하게 만들거나 오염시키는 것보다 좋을 것입니다. 아마도 콘텐츠 제공자 내에서 스트림을 닫는 것을 감지하여 스스로 안전하게 정리할 수있는 방법이 있습니까? (gmail/standaed) 이메일 클라이언트에 첨부 파일을 보내는 것에 관심이 있습니다. 이러한 문제가 발생할 수있는 다른 장소가 있다고 확신합니다. – hannasm
예, MemoryFile.java에는 현재'ParcelFileDescriptor getParcelFileDescriptor()'메소드가 있습니다. 이것은 도넛 (Donut)의 일환으로 이루어졌지만 제프 (Jeff)가 말했듯이 아직 완성되지 않았습니다. "개념"이 적어도 작동하고 리플렉션을 사용하여 현재 완료 될 수 있음을 확인했습니다. 그것은 매우 더럽지 만 권장하지는 않습니다. 불행히도, 'Memory.isMemoryFile()'는 소켓이 PFD 나 메모리 파일이 아니기 때문에'Memory.isMemoryFile()'이 예외를 던 졌기 때문에'ParcelFileDescriptor.fromSocket()'도 사용할 수 없습니다. – Joe
MemoryFile을 사용할 때주의해야합니다. 올바르게 이해하면 파일의 전체 내용을 메모리에 저장하므로 사용 가능한 메모리보다 큰 파일을 사용할 수 없습니다. –