首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DocumentFile RandomAccessFile

DocumentFile RandomAccessFile
EN

Stack Overflow用户
提问于 2015-03-06 18:58:15
回答 4查看 2K关注 0票数 5

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

我知道通过getUri获取InputStream是可能的

代码语言:javascript
复制
InputStream inputStream = getContentResolver().openInputStream(DocumentFile.getUri());

但是我需要一个RandomAccessFile

谢谢你的帮忙,

延斯

EN

回答 4

Stack Overflow用户

发布于 2015-05-01 16:10:56

对于SDK 21 (棒棒糖)SD卡上的文件进行随机读/写访问的唯一方法似乎如下所示:

代码语言:javascript
复制
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;
    }
}
票数 2
EN

Stack Overflow用户

发布于 2015-12-05 22:50:01

从API级别21 (棒棒糖)开始,这可能是一个低级别的替换:

代码语言:javascript
复制
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。

票数 2
EN

Stack Overflow用户

发布于 2015-03-06 19:16:58

创建文本文件,其中包含您要随机打开的所有文件的路径

代码语言:javascript
复制
in = new BufferedReader(new FileReader("randomfilepaths.txt"));

创建一个函数来获取一个随机文件的路径,这可以由readLine()来完成。

代码:

代码语言:javascript
复制
protected String getRandomPath() {
     String returnval = null;
 try{
   if((returnval = in.readLine()) == null){
    in.close();
    moreFiles = false;
   }
 }catch(Exception e){}
 return returnval;
}

这里的returnval将包含一个随机文件的字符串路径。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28897329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档