有没有办法从给定的DocumentFile中获取RandomAccessFile
我知道通过getUri获取InputStream是可能的
InputStream inputStream = getContentResolver().openInputStream(DocumentFile.getUri());但是我需要一个RandomAccessFile
谢谢你的帮忙,
延斯
发布于 2015-05-01 16:10:56
对于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;
}
}发布于 2015-12-05 22:50:01
从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。
发布于 2015-03-06 19:16:58
创建文本文件,其中包含您要随机打开的所有文件的路径
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将包含一个随机文件的字符串路径。
https://stackoverflow.com/questions/28897329
复制相似问题