2015-03-06 10 views
5

C'è un modo per ottenere un RandomAccessFile da un dato DocumentFile?DocumentFile RandomAccessFile

So che è possibile ottenere un InputStream via getUri

InputStream inputStream = getContentResolver().openInputStream(DocumentFile.getUri()); 

Ma ho bisogno di un RandomAccessFile

Grazie per il vostro aiuto,

Jens

risposta

0

Creare file di testo che contiene il percorso di tutti i file che si desidera aprire a caso

in = new BufferedReader(new FileReader("randomfilepaths.txt")); 

Creare una funzione per ottenere il percorso di un file casuale può essere eseguita dallo readLine().

codice:

protected String getRandomPath() { 
    String returnval = null; 
try{ 
    if((returnval = in.readLine()) == null){ 
    in.close(); 
    moreFiles = false; 
    } 
}catch(Exception e){} 
return returnval; 
} 

Qui returnval conterrà il percorso stringa in un file casuale.

+0

stavo parlando della classe RandomAccessFile http://developer.android.com/reference/java/io/RandomAccessFile.html – JensSommer

+0

Date un'occhiata a questa soluzione http://stackoverflow.com/a/28805474/753575 – isabsent

2

Sembra che l'unico modo per ottenere una lettura casuale/scrittura a un file su scheda SD per SDK 21 (Lollipop) è il seguente:

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; 
    } 
} 
+1

Questo non funziona se si apre un file di documento in modalità di aggiunta mentre continua a scrivere alla fine del file indipendentemente dalla posizione di FileChannel –

2

Dal livello di API 21 (Lollipop), questo potrebbe una sostituzione basso livello:

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); 

altri metodi basso livello come read(fd, ...)?write(fd, ...)fstat(fd) può essere trovato in android.system.OS, anche.

Assicurati di disporre di un uri con accesso in lettura/scrittura.