2011-12-12 11 views
49

Ho bisogno di conoscere il percorso stringa di a un file su cartella Assets, perché sto utilizzando una mappa api che ha bisogno di ricevere un percorso di stringa, e le mie mappe devono essere conservati in cartella AssetsCome ottenere la stringa Percorso Android su un file nella cartella Risorse?

Questo è il codice sto cercando:

MapView mapView = new MapView(this); 
    mapView.setClickable(true); 
    mapView.setBuiltInZoomControls(true); 
    mapView.setMapFile("file:///android_asset/m1.map"); 
    setContentView(mapView); 

Qualcosa sta andando male con "file:///android_asset/m1.map" perché la mappa non viene caricata.

Quale è il file percorso stringa corretta per il m1.map file memorizzato sul mio cartella Assets?

Grazie

EDIT per Dimitru: Questo codice non funziona, non riesce a is.read(buffer); con IOException

 try { 
      InputStream is = getAssets().open("m1.map"); 
      int size = is.available(); 
      byte[] buffer = new byte[size]; 
      is.read(buffer); 
      is.close(); 
      text = new String(buffer); 
     } catch (IOException e) {throw new RuntimeException(e);} 

risposta

74

per quanto ne so i file nella directory degli asset non si ottiene spacchettato. Invece, vengono letti direttamente dal file APK (ZIP).

Quindi, davvero non si può fare roba che si aspetta un file accettare un 'file' di asset.

Invece, dovrete estrarre il bene e scrivere in un file separato, come suggerisce Dumitru:

File f = new File(getCacheDir()+"/m1.map"); 
    if (!f.exists()) try { 

    InputStream is = getAssets().open("m1.map"); 
    int size = is.available(); 
    byte[] buffer = new byte[size]; 
    is.read(buffer); 
    is.close(); 


    FileOutputStream fos = new FileOutputStream(f); 
    fos.write(buffer); 
    fos.close(); 
    } catch (Exception e) { throw new RuntimeException(e); } 

    mapView.setMapFile(f.getPath()); 
+2

ottengo eccezione: java.io.FileNotFoundException: m1.map – NullPointerException

+0

l'eccezione si trova sulla linea InputStream è = getAssets() .open ("m1.map"); – NullPointerException

+1

ok, risolto, ma ora ho avuto la stessa eccezione che con Dumitru risposta, 12-12 15: 06: 41,452: DEBUG/bene (3760): Dati supera UNCOMPRESS_DATA_MAX (3.491.923 contro 1.048.576) – NullPointerException

8

Date un'occhiata al ReadAsset.java da campioni di API che vengono con l'SDK.

 try { 
     InputStream is = getAssets().open("read_asset.txt"); 

     // We guarantee that the available method returns the total 
     // size of the asset... of course, this does mean that a single 
     // asset can't be more than 2 gigs. 
     int size = is.available(); 

     // Read the entire asset into a local byte buffer. 
     byte[] buffer = new byte[size]; 
     is.read(buffer); 
     is.close(); 

     // Convert the buffer into a string. 
     String text = new String(buffer); 

     // Finally stick the string into the text view. 
     TextView tv = (TextView)findViewById(R.id.text); 
     tv.setText(text); 
    } catch (IOException e) { 
     // Should never happen! 
     throw new RuntimeException(e); 
    } 
+0

Non funziona per me, la linea is.read (buffer); mi dà IOException – NullPointerException

+0

ho modificato la mia domanda con il nuovo codice, controllare che non si prega di – NullPointerException

+0

Cosa eccezione esattamente vuol buttare? –

5

È possibile utilizzare questo metodo.

public static File getRobotCacheFile(Context context) throws IOException { 
     File cacheFile = new File(context.getCacheDir(), "robot.png"); 
     try { 
      InputStream inputStream = context.getAssets().open("robot.png"); 
      try { 
       FileOutputStream outputStream = new FileOutputStream(cacheFile); 
       try { 
        byte[] buf = new byte[1024]; 
        int len; 
        while ((len = inputStream.read(buf)) > 0) { 
         outputStream.write(buf, 0, len); 
        } 
       } finally { 
        outputStream.close(); 
       } 
      } finally { 
       inputStream.close(); 
      } 
     } catch (IOException e) { 
      throw new IOException("Could not open robot png", e); 
     } 
     return cacheFile; 
    } 

si dovrebbe mai usare inputStream.available() in questi casi. Restituisce solo i byte che vengono memorizzati nel buffer. Il metodo con .available() non funzionerà mai con i file più grandi e non funzionerà affatto su alcuni dispositivi.

1

solo per aggiungere il soluzione perfetta di Jacek. Se stai cercando di farlo in Kotlin, non funzionerà immediatamente. Invece, ti consigliamo di utilizzare questo:

@Throws(IOException::class) 
fun getSplashVideo(context: Context): File { 
    val cacheFile = File(context.cacheDir, "splash_video") 
    try { 
     val inputStream = context.assets.open("splash_video") 
     val outputStream = FileOutputStream(cacheFile) 
     try { 
      inputStream.copyTo(outputStream) 
     } finally { 
      inputStream.close() 
      outputStream.close() 
     } 
    } catch (e: IOException) { 
     throw IOException("Could not open splash_video", e) 
    } 
    return cacheFile 
} 
Problemi correlati