如果你想要一些跨进程使用,你可以使用以下模糊的方法:MemoryFile
import android.os.MemoryFile;
import android.os.ParcelFileDescriptor;
import java.io.FileDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MemoryFileUtil {
private static final Method sMethodGetParcelFileDescriptor;
private static final Method sMethodGetFileDescriptor;
static {
sMethodGetParcelFileDescriptor = get("getParcelFileDescriptor");
sMethodGetFileDescriptor = get("getFileDescriptor");
}
public static ParcelFileDescriptor getParcelFileDescriptor(MemoryFile file) {
try {
return (ParcelFileDescriptor) sMethodGetParcelFileDescriptor.invoke(file);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
public static FileDescriptor getFileDescriptor(MemoryFile file) {
try {
return (FileDescriptor) sMethodGetFileDescriptor.invoke(file);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
private static Method get(String name) {
try {
return MemoryFile.class.getDeclaredMethod(name);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
}
您应该关注的是可以从 的实现返回的方法。#getParcelFileDescriptor(MemoryFile)
ContentProvider#openFile(Uri, String)