DocumentFile RandomAccessFile

5

有没有办法从给定的 DocumentFile 中获取一个 RandomAccessFile

我知道可以通过 getUri 获取InputStream。

InputStream inputStream = getContentResolver().openInputStream(DocumentFile.getUri());

但是我需要一个 RandomAccessFile

感谢您的帮助,

Jens

4个回答

3
似乎在SDK 21(棒棒糖)中,获取SD卡上文件的随机读/写访问的唯一方法是按以下方式进行:
import android.content.Context;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import com.jetico.bestcrypt.FileManagerApplication;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class SecondaryCardChannel {//By analogy with the java.nio.channels.SeekableByteChannel
    private FileChannel fileChannel;
    private ParcelFileDescriptor pfd;
    private boolean isInputChannel;
    private long position;

    public SecondaryCardChannel(Uri treeUri, Context context) {
        try {
            pfd = context.getContentResolver().openFileDescriptor(treeUri, "rw");
            FileInputStream fis = new FileInputStream(pfd.getFileDescriptor());
            fileChannel = fis.getChannel();
            isInputChannel = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public int read(ByteBuffer buffer) {
        if (!isInputChannel) {
            try {
                fileChannel.close();
                FileInputStream fis = new FileInputStream(pfd.getFileDescriptor());
                fileChannel = fis.getChannel();
                isInputChannel = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            fileChannel.position(position);
            int bytesRead = fileChannel.read(buffer);
            position = fileChannel.position();
            return bytesRead;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public int write(ByteBuffer buffer) {
        if (isInputChannel) {
            try {
                fileChannel.close();
                FileOutputStream fos = new FileOutputStream(pfd.getFileDescriptor());
                fileChannel = fos.getChannel();
                isInputChannel = false;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            fileChannel.position(position);
            int bytesWrite = fileChannel.write(buffer);
            position = fileChannel.position();
            return bytesWrite;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public long position() throws IOException {
        return position;
    }

    public SecondaryCardChannel position(long newPosition) throws IOException {
        position = newPosition;
        return this;
    }

    public long size() throws IOException {
        return fileChannel.size();
    }

    public SecondaryCardChannel truncate(long size) throws IOException {
        fileChannel.truncate(size);
        return this;
    }
}

1
如果您以追加模式打开文档文件,则此方法不起作用,因为它会在文件的末尾继续写入,而不考虑FileChannel位置。 - Rahul Verma

2
自API 21(棒棒糖)开始,这可能是低级替代方案:
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "rw");
FileDescriptor fd = pfd.getFileDescriptor();
// seek to offset 10 from beginning of file
android.system.Os.lseek(fd, 10, OsConstants.SEEK_SET);

其他低级方法,例如read(fd, ...)? write(fd, ...) fstat(fd)也可以在android.system.OS中找到。

确保您拥有具有读/写访问权限的URI。


-1
创建一个文本文件,其中包含您想要随机打开的所有文件的路径。
in = new BufferedReader(new FileReader("randomfilepaths.txt"));

创建一个函数,用于获取一个随机文件的路径,可以通过readLine()来实现。
代码:
protected String getRandomPath() {
     String returnval = null;
 try{
   if((returnval = in.readLine()) == null){
    in.close();
    moreFiles = false;
   }
 }catch(Exception e){}
 return returnval;
}

在这里,returnval将包含指向随机文件的字符串路径。


我在谈论RandomAccessFile类,它可以在http://developer.android.com/reference/java/io/RandomAccessFile.html找到。 - JensSommer

-1
实际上,Google忘记添加这种功能或者决定不这样做。因此,在使用存储访问框架时,无法获取RandomAccessFile
但是,如果您的代码需要RandomAccessFile,如果可能的话,您可以创建一个超类,比如MyRandomAccessFile,它的子类基于RandomAccessFile(和File)或从中获取的InputStreamOutputStream。我使用了这种方法来适应SAF的JaudioTagger。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接