[英]DocumentFile RandomAccessFile
有没有办法从给定的DocumentFile
获取RandomAccessFile
?
我知道可以通过getUri
获得 InputStream
InputStream inputStream = getContentResolver().openInputStream(DocumentFile.getUri());
但我需要一个RandomAccessFile
谢谢你的帮助,
延斯
似乎对 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;
}
}
从 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。
创建文本文件,其中包含要随机打开的所有文件的路径
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 将包含一个随机文件的字符串路径。
事实上,谷歌忘记添加这种功能或决定不添加。 因此,在使用Storage Access Framework时不可能获得RandomAccessFile 。
但是,如果您的代码中需要RandomAccessFile ,则可能的话,您可以创建一个像MyRandomAccessFile这样的超类及其基于 RandomAccessFile(和 File)或InputStream和OutputStream 的子类,您可以从DocumentFile获取。 我使用这种方法使 JaudioTagger 适应 SAF。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.