【问题标题】:How to copy a zip map file to /sdcard/osmdroid/ (internal storage)如何将 zip 映射文件复制到 /sdcard/osmdroid/(内部存储)
【发布时间】:2014-01-19 13:20:41
【问题描述】:

我使用 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);
     }
 }

【问题讨论】:

  • 那么您的问题是从资产复制到 SD 卡还是嵌入此压缩图块?
  • 你还没有解释到底什么不适合你。
  • 不清楚你在问什么
  • 我想将 zip 文件从资产复制到 sdcard。我需要一堂课来做到这一点,但我不知道该怎么做

标签: android openstreetmap osmdroid


【解决方案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 卡。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 问题是我不知道我必须实现什么样的代码来复制 /sdcard/osmdroid 中的文件
    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 2015-02-16
    • 2011-06-25
    相关资源
    最近更新 更多