1

デバイスに保存されているファイルに実際にはないデータを、デバイスに保存されているファイルのように見せることができるプログラムを開発しようとしています。目的に合わせて微調整する必要がある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.
}
4

0 に答える 0