Java 我如何使用ContentProvider来处理数据到文件系统的实际读写?

Java 我如何使用ContentProvider来处理数据到文件系统的实际读写?,java,android,filesystems,android-contentprovider,Java,Android,Filesystems,Android Contentprovider,我正在尝试开发一个程序,使存储在设备上的文件中实际上不存在的数据看起来像是存储在设备上的文件。我得到的印象是,我需要调整ContentProvider的一部分以适应我的目的是openFile命令。当然,openFile生成了一个ParcelFileDescriptor。这是我遇到麻烦的地方,因为我在ParcelFileDescriptor中找不到任何接收或返回字节、字节数组或文件数据的命令 例如,假设我想创建一个在后台运行的应用程序,并欺骗我默认的Android文件浏览器,使其认为在我的“Doc

我正在尝试开发一个程序,使存储在设备上的文件中实际上不存在的数据看起来像是存储在设备上的文件。我得到的印象是,我需要调整ContentProvider的一部分以适应我的目的是openFile命令。当然,openFile生成了一个ParcelFileDescriptor。这是我遇到麻烦的地方,因为我在ParcelFileDescriptor中找不到任何接收或返回字节、字节数组或文件数据的命令

例如,假设我想创建一个在后台运行的应用程序,并欺骗我默认的Android文件浏览器,使其认为在我的“Documents”文件夹中有一个名为“Phantom.txt”的文件,即使存储空间中实际上不存在这样的文件。然后,当用户打开这个不存在的文件时,后台应用程序会提供应该包含的数据(但显然不能包含,因为它根本不存在)。此应用程序的代码必须包括以下代码:

public class myContentProvider extends ContentProvider {

    public ParcelFileDescriptor openFile(Uri uri, String mode) {
        if(uri.toString()=="file:\\\\Documents\\Phantom.txt") {
            return new myPFD();
        } else {
            return super.openFile(uri, mode);
        }
    }
}

public class myPFD extends ParcelFileDescriptor {

    private final String filedata = "This file doesn't really exist!\nThis text is actually part of an application!\nIt isn't saved to your flash memory!\nWeird, isn't it?";
    private int readPosition;

    public myPFD() {
        super(ParcelFileDescriptor.open(File.createTempFile("uselessParameterFiller", ".tmp"), ParcelFileDescriptor.MODE_READ_ONLY));
        readPosition = 0;
    }

    //NOTE: There is no actual read(byte [] b) command in ParcelFileDescriptor,
    //But this illustrates the kind of functionality I'm looking to achieve.
    //I just need to replace the nonexistant ParcelFileDescriptor.read(byte [] b)
    //with whatever Android actually uses to read the actual data of the file.

    public int read(byte [] b) {
        byte [] dataAsBytes = filedata.getBytes();
        if(readPosition<dataAsBytes.length) {
            int x = readPosition;
            for(x=readPosition;x<Math.min(readPosition + b.length, dataAsBytes.length);x++){
                b[x - readPosition] = dataAsBytes[x];
            }
            int output = x - readPosition;
            readPosition = x;
            return output;
        } else {
            return -1;
        }
    }

    //For simplicity's sake, I have forgone writing customizations of other reading
    //commands such as "int read()", and "int read(byte [], int, int)". Also the
    //command "seek(long)", for assigning a reading position.
    //I have also neglected to show any commands that would allow me to recieve and
    //handle any data that an external app might try to write to this nonexistant
    //file. Though I would also like to know how to do this.
}
公共类myContentProvider扩展ContentProvider{
公共ParcelFileDescriptor openFile(Uri,字符串模式){
如果(uri.toString()=“文件:\\\\Documents\\Phantom.txt”){
返回新的myPFD();
}否则{
返回super.openFile(uri,模式);
}
}
}
公共类myPFD扩展了ParcelFileDescriptor{
private final String filedata=“此文件实际上不存在!\n此文本实际上是应用程序的一部分!\n它没有保存到闪存中!\n是否存在?”;
私人位置;
公共多年筹资框架(){
super(ParcelFileDescriptor.open(File.createTempFile(“uselessParameterFiller”,“.tmp”),ParcelFileDescriptor.MODE_READ_ONLY));
readPosition=0;
}
//注意:ParcelFileDescriptor中没有实际的读取(字节[]b)命令,
//但这说明了我希望实现的功能。
//我只需要替换不存在的ParcelFileDescriptor.read(字节[]b)
//无论Android实际使用什么来读取文件的实际数据。
公共整数读取(字节[]b){
byte[]dataAsBytes=filedata.getBytes();
如果(重新定位