2014-01-19 16 views
2

我使用MOBAC(Osmdroid zip格式,OpenStreetMap MapQuest源代碼)創建了一個地圖。 現在我有我的android項目的資產文件夾中的這個zip文件(該文件的名稱是prova.zip),我需要在我的手機(內部存儲器)內複製到/ sdcard/osmdroid/ 我發現了一些類網絡,但他們不工作,也許我做錯了什麼。如何將zip地圖文件複製到/ sdcard/osmdroid /(內部存儲)

可以請別人幫助我如何找出這個問題?

謝謝

MainActivity.java

public class MainActivity extends Activity { 
MyItemizedOverlay myItemizedOverlay = null; 
MyLocationOverlay myLocationOverlay = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final MapView mapView = (MapView) findViewById(R.id.mapview); 
    mapView.setTileSource(TileSourceFactory.MAPQUESTOSM); 
    mapView.setBuiltInZoomControls(true); 
    mapView.setMultiTouchControls(true); 

    IMapController mapController = mapView.getController(); 
    mapController.setZoom(18); 

    ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this); 
    mapView.getOverlays().add(myScaleBarOverlay); 

    GeoPoint startPoint = new GeoPoint(0, 0); 

    myLocationOverlay = new MyLocationOverlay(this, mapView); 
    mapView.getOverlays().add(myLocationOverlay); 

    myLocationOverlay.runOnFirstFix(new Runnable() { 
    public void run() { 
    mapView.getController().animateTo(myLocationOverlay.getMyLocation()); 
    } 
    });  
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    myLocationOverlay.enableMyLocation(); 
    myLocationOverlay.enableCompass(); 
    myLocationOverlay.enableFollowLocation(); 
} 

@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    myLocationOverlay.disableMyLocation(); 
    myLocationOverlay.disableCompass(); 
    myLocationOverlay.disableFollowLocation(); 
} 
} 

我試圖實現這個代碼,但它不能在MOBAC工作

private void copyAssets() { 
    AssetManager assetManager = getAssets(); 
    String[] files = null; 

    try { 
    files = assetManager.list(""); 
    } 

    catch (IOException e) { 
    Log.e("tag", "Failed to get asset file list.", e); 
    } 

    for(String prova : files) { 
    InputStream in = null; 
    OutputStream out = null; 

    try { 
    in = assetManager.open(prova); 
    File outFile = new File(getExternalFilesDir(null)+"/sdcard/osmdroid/", prova); 
    out = new FileOutputStream(outFile); 
    copyFile(in, out); 
    in.close(); 
    in = null; 
    out.flush(); 
    out.close(); 
    out = null; 
    } 

    catch(IOException e) { 
    Log.e("tag", "Failed to copy asset file: " + prova, e); 
    }  
    } 
} 

private void copyFile(InputStream in, OutputStream out) throws IOException { 
    byte[] buffer = new byte[1024]; 
    int read; 

    while((read = in.read(buffer)) != -1){ 
    out.write(buffer, 0, read); 
    } 
} 
+1

那麼你的問題是從資產複製到SD卡或嵌入此壓縮瓷磚一般? – MaM

+0

你還沒有解釋什麼不適合你。 – scai

+0

不清楚你在問什麼 –

回答

0

嘗試創建「OSMAND瓷磚存儲」並在/sdcard/osmdroid/tiles文件夾中複製結果文件夾。

+0

問題是我不知道我必須實現哪種代碼來複制/ sdcard/osmdroid中的文件 – Bombolo

1

我想這應該可以幫助您:

public class MainActivity extends Activity { 
MyItemizedOverlay myItemizedOverlay = null; 
MyLocationOverlay myLocationOverlay = null; 

    protected void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    new CopyData().execute(); 
    } 
} 
public class CopyData extends AsyncTask<Void, Void, String> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      Toast.makeText(getApplicationContext(), "Copy Started", Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     protected String doInBackground(Void... params) { 
      copyAssets(); 
      return "Done"; 

     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      Toast.makeText(getApplicationContext(), "Copy Complete", Toast.LENGTH_LONG).show(); 
     } 

    } 


private void copyAssets() { 

InputStream in = null; 
OutputStream out = null; 
try { 

    in = getAssets().open("prova.zip"); 

    Log.i(TAG, ": "+Environment.getExternalStorageDirectory()); 
    File dir = new File(Environment.getExternalStorageDirectory(), 
      "osmdroid"); 
    Log.i(TAG, "isExist : " + dir.exists()); 
    if (!dir.exists()) 
     dir.mkdirs(); 
    File fileZip = new File(dir, "prova.zip"); 
    Log.i(TAG, "isExist : " + fileZip.exists()); 

    out = new FileOutputStream(fileZip); 
    copyFile(in, out); 
    in.close(); 
    in = null; 
    out.flush(); 
    out.close(); 
    out = null; 
} catch (IOException e) { 
    Log.e("tag", "Failed to copy asset file: " + e.getMessage()); 
} 
} 

private void copyFile(InputStream in, OutputStream out) throws IOException { 
    byte[] buffer = new byte[1024]; 
    int read; 
    while ((read = in.read(buffer)) != -1) { 
     out.write(buffer, 0, read); 
    } 
} 

權限:

而且不要忘記添加在未經許可到mainfest.xml文件

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

這將複製您PROVA .zip文件從資源文件夾複製到SD卡。 希望得到這個幫助。

相關問題