如何通过Lollipop提供的API实现对SD卡上文件的随机访问?

11

我的应用程序使用Java类RandomAccessFile通过实现SeekableByteChannel接口在SD卡上随机读写字节。现在我需要使用新的Lollipop API重写它以适配Android 5.0。

我已经找到了唯一的方法来进行读取:

InputStream inputStream = getContentResolver().openInputStream(uri);

并写下:

ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "w");
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());

从/向新API的文件中读取/写入。

我希望能够在某个随机位置设置通道并读/写字节到该位置。在新的SDK 21中是否有可能做到这一点?新的SDK是否意味着获得通道的这种方式:

FieInputChannel fieInputChannel = fileInputStream.getChannel();
FieOutputChannel fieOutputChannel = fileOutputStream.getChannel();

还是其他方法?

1个回答

11

似乎在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;
    }
}

编辑于 2017/03/15: 优化后的版本如下

import android.content.Context;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

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 {
    private ParcelFileDescriptor pfdInput, pfdOutput;
    private FileInputStream fis;
    private FileOutputStream fos;
    private long position;

    public SecondaryCardChannel(Uri treeUri, Context context) {
        try {
            pfdInput = context.getContentResolver().openFileDescriptor(treeUri, "r");
            pfdOutput = context.getContentResolver().openFileDescriptor(treeUri, "rw");
            fis = new FileInputStream(pfdInput.getFileDescriptor());
            fos = new FileOutputStream(pfdOutput.getFileDescriptor());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public int read(ByteBuffer buffer) {
        try {
            FileChannel fch = fis.getChannel();
            fch.position(position);
            int bytesRead = fch.read(buffer);
            position = fch.position();
            return bytesRead;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public int write(ByteBuffer buffer) {
        try {
            FileChannel fch = fos.getChannel();
            fch.position(position);
            int bytesWrite = fch.write(buffer);
            position = fch.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 fis.getChannel().size();
    }

    public void force(boolean metadata) throws IOException {
        fos.getChannel().force(metadata);
        pfdOutput.getFileDescriptor().sync();
    }

    public long truncate(long size) throws Exception {
        FileChannel fch = fos.getChannel();
        try {
            fch.truncate(size);
            return fch.size();
        } catch (Exception e){ // Attention! Truncate is broken on removable SD card of Android 5.0
            e.printStackTrace();
            return -1;
        }
    }

    public void close() throws IOException {
        FileChannel fch = fos.getChannel();
        fch.close();

        fos.close();
        fis.close();
        pfdInput.close();
        pfdOutput.close();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接