5

有什么办法RandomAccessFile从给定的DocumentFile

我知道可以通过 InputStreamgetUri

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

但我需要一个RandomAccessFile

谢谢你的帮助,

延斯

4

4 回答 4

2

似乎对 SDK 21 (Lollipop) 的 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;
    }
}
于 2015-05-01T08:10:55.843 回答
2
于 2015-12-05T14:50:01.237 回答
-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 将包含随机文件的字符串路径。

于 2015-03-06T11:16:58.097 回答
-1

事实上,谷歌忘记添加这种功能或决定不添加。因此,在使用Storage Access Framework时无法获得RandomAccessFile

但是,如果您的代码中需要RandomAccessFile,如果可能,您可以创建一个像MyRandomAccessFile这样的超类,它的子类基于 RandomAccessFile(和 File)或InputStreamOutputStream,您可以从DocumentFile获得。我使用该方法使 JaaudioTagger 适应 SAF。

于 2020-10-27T22:13:15.807 回答