[英]DocumentFile RandomAccessFile
Is there any way get a RandomAccessFile
from a given DocumentFile
?有没有办法从给定的
DocumentFile
获取RandomAccessFile
?
I know it is possible to get an InputStream via getUri
我知道可以通过
getUri
获得 InputStream
InputStream inputStream = getContentResolver().openInputStream(DocumentFile.getUri());
But I need a RandomAccessFile
但我需要一个
RandomAccessFile
Thanks for your help,谢谢你的帮助,
Jens延斯
It seems the only way to get a random read/write access to a file on SD card for SDK 21 (Lollipop) is as follows:似乎对 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;
}
}
Since API level 21 (Lollipop), this might be a low level replacement:从 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);
other low-level methods like read(fd, ...)?
其他低级方法,如
read(fd, ...)?
write(fd, ...)
fstat(fd)
can be found in android.system.OS , too. write(fd, ...)
fstat(fd)
也可以在android.system.OS 中找到。
Be sure you have an uri that has read/write access.确保您有一个具有读/写访问权限的 uri。
Create text file which contains path of all files that you want to open at random创建文本文件,其中包含要随机打开的所有文件的路径
in = new BufferedReader(new FileReader("randomfilepaths.txt"));
Create a function for getting path of one random file this could be done by the readLine()
.创建一个函数来获取一个随机文件的路径,这可以通过
readLine()
来完成。
code:代码:
protected String getRandomPath() {
String returnval = null;
try{
if((returnval = in.readLine()) == null){
in.close();
moreFiles = false;
}
}catch(Exception e){}
return returnval;
}
Here returnval will contain the String path to a random file.这里 returnval 将包含一个随机文件的字符串路径。
In fact Google forgot to add that kind of function or decided not to do it.事实上,谷歌忘记添加这种功能或决定不添加。 Thus it is not possible to get a RandomAccessFile when using the Storage Access Framework .
因此,在使用Storage Access Framework时不可能获得RandomAccessFile 。
However, if you need a RandomAccessFile in your code, you might, if possible, create a super class like MyRandomAccessFile with its subclasses based on either RandomAccessFile (and File) or on InputStream and OutputStream you can get from the DocumentFile .但是,如果您的代码中需要RandomAccessFile ,则可能的话,您可以创建一个像MyRandomAccessFile这样的超类及其基于 RandomAccessFile(和 File)或InputStream和OutputStream 的子类,您可以从DocumentFile获取。 I used that method to adapt JaudioTagger to SAF.
我使用这种方法使 JaudioTagger 适应 SAF。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.