2012-04-06 15 views
30

Sto cercando di ottenere il contenuto del file in byte nell'applicazione Android. Ho ottenuto il file nella scheda SD ora voglio ottenere il file selezionato in byte. Ho cercato su Google ma nessun tale successo. Please helpAndroid: come leggere il file in byte?

Di seguito è riportato il codice per ottenere file con estensione. Attraverso questo ottengo file e mostra in spinner. Sulla selezione del file voglio ottenere il file in byte.

private List<String> getListOfFiles(String path) { 

    File files = new File(path); 

    FileFilter filter = new FileFilter() { 

     private final List<String> exts = Arrays.asList("jpeg", "jpg", "png", "bmp", "gif","mp3"); 

     public boolean accept(File pathname) { 
     String ext; 
     String path = pathname.getPath(); 
     ext = path.substring(path.lastIndexOf(".") + 1); 
     return exts.contains(ext); 
     } 
    }; 

    final File [] filesFound = files.listFiles(filter); 
    List<String> list = new ArrayList<String>(); 
    if (filesFound != null && filesFound.length > 0) { 
     for (File file : filesFound) { 
     list.add(file.getName()); 
     } 
    } 
    return list; 
} 

risposta

73

qui si tratta di un semplice:

File file = new File(path); 
int size = (int) file.length(); 
byte[] bytes = new byte[size]; 
try { 
    BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file)); 
    buf.read(bytes, 0, bytes.length); 
    buf.close(); 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

Aggiungi autorizzazione manifest.xml:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
+6

Si noti che 'buf.read()' non può leggere il numero di byte richiesti. Vedere javadoc qui: http://docs.oracle.com/javase/6/docs/api/java/io/BufferedInputStream.html#read (byte [], int, int) – vaughandroid

+1

Non dimenticare in manifest. – CoolMind

+0

Se si intende leggere l'intero file con un solo colpo, non è necessario avvolgere FileInputStream in un BufferedInputStream -indeed, in tal modo si utilizzerà più memoria e si rallenteranno i dati che verranno copiati inutilmente. – Clyde

14

Ecco una soluzione che garantisce intero file verrà letto, che non richiede le librerie e è efficiente:

byte[] fullyReadFileToBytes(File f) throws IOException { 
    int size = (int) f.length(); 
    byte bytes[] = new byte[size]; 
    byte tmpBuff[] = new byte[size]; 
    FileInputStream fis= new FileInputStream(f);; 
    try { 

     int read = fis.read(bytes, 0, size); 
     if (read < size) { 
      int remain = size - read; 
      while (remain > 0) { 
       read = fis.read(tmpBuff, 0, remain); 
       System.arraycopy(tmpBuff, 0, bytes, size - remain, read); 
       remain -= read; 
      } 
     } 
    } catch (IOException e){ 
     throw e; 
    } finally { 
     fis.close(); 
    } 

    return bytes; 
} 

NOTA: presuppone che la dimensione del file sia inferiore a MAX_INT byte, è possibile aggiungere la gestione per questo se lo si desidera.

+1

Grazie. Sostituire 'jsonFile' con 'f'. – CoolMind

+0

Si crea molta confusione chiamando la dimensione del buffer e le dimensioni del file con lo stesso nome: "size". In questo modo avrai una dimensione del buffer della dimensione del file che significa nessun buffer, il tuo ciclo non verrà mai eseguito. – FlorianB

+0

@FlorianB La speranza è che il ciclo non venga mai eseguito. nel migliore dei casi, l'intero file verrà letto nella prima chiamata a 'fis.read (byte, 0, dimensione);' ... il ciclo è lì solo se il file è troppo grande e il sistema non lo fa t leggere l'intero file, nel qual caso entriamo nel ciclo e leggiamo il resto in chiamate consecutive a fis.read(). file.read() non garantisce che tutti i byte che sono stati richiesti possano essere letti in una chiamata, e il valore restituito è il numero di byte letti in modo ACTUALLY. – Siavash

0

Tu può anche fare in questo modo:

byte[] getBytes (File file) 
{ 
    FileInputStream input = null; 
    if (file.exists()) try 
    { 
     input = new FileInputStream (file); 
     int len = (int) file.length(); 
     byte[] data = new byte[len]; 
     int count, total = 0; 
     while ((count = input.read (data, total, len - total)) > 0) total += count; 
     return data; 
    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
    } 
    finally 
    { 
     if (input != null) try 
     { 
      input.close(); 
     } 
     catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
    } 
    return null; 
} 
5

Dal accettato la BufferedInputStream#read non è garantita a leggere tutto, piuttosto che tenere traccia del buffer di dimensioni me stesso, ho usato questo approccio:

byte bytes[] = new byte[(int) file.length()]; 
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); 
    DataInputStream dis = new DataInputStream(bis); 
    dis.readFully(bytes); 

blocchi fino una lettura completa è completa e non richiede importazioni aggiuntive.

+0

Dis mi ha davvero aiutato molto –

0

Un semplice InputStream farà

byte[] fileToBytes(File file){ 
    byte[] bytes = new byte[0]; 
    try(FileInputStream inputStream = new FileInputStream(file)) { 
     bytes = new byte[inputStream.available()]; 
     //noinspection ResultOfMethodCallIgnored 
     inputStream.read(bytes); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return bytes; 
}