如何将zip地图文件复制到/ sdcard / osmdroid /(内部存储)

时间:2014-01-19 13:20:42

标签: android openstreetmap osmdroid

我使用MOBAC(Osmdroid zip格式,OpenStreetMap MapQuest源)创建了一个地图。

现在我的Android项目的assets文件夹中有这个ZIP文件(文件的名称是prova.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();
    }
}

我尝试实现此代码,但它不起作用:

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

2 个答案:

答案 0 :(得分: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卡。

答案 1 :(得分:0)

在MOBAC中尝试创建“OSMAND磁贴存储”,并将结果文件夹复制到/sdcard/osmdroid/tiles文件夹。