Android 如何将zip映射文件复制到/sdcard/osmdroid/(内部存储器)
我使用MOBAC(Osmdroid zip格式,OpenStreetMap MapQuest源代码)创建了一个地图 现在我在Android项目的资产文件夹中有了这个ZIP文件(文件名为prova.ZIP),我需要在手机内部(内存)复制到Android 如何将zip映射文件复制到/sdcard/osmdroid/(内部存储器),android,openstreetmap,osmdroid,Android,Openstreetmap,Osmdroid,我使用MOBAC(Osmdroid zip格式,OpenStreetMap MapQuest源代码)创建了一个地图 现在我在Android项目的资产文件夹中有了这个ZIP文件(文件名为prova.ZIP),我需要在手机内部(内存)复制到/sdcard/osmdroid/。 我在网上找到了一些课程,但它们不起作用,或者我做错了什么 我该如何解决这个问题 MainActivity.java 我尝试实现此代码,但它不起作用: private void copyAssets() { AssetM
/sdcard/osmdroid/
。
我在网上找到了一些课程,但它们不起作用,或者我做错了什么
我该如何解决这个问题
MainActivity.java
我尝试实现此代码,但它不起作用:
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);
}
}
请尝试在MOBAC中创建“OSMAND磁贴存储”,并将结果文件夹复制到
/sdcard/osmdroid/tiles
文件夹中。我认为这会帮助您:
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);
}
}
公共类MainActivity扩展活动{
MyItemizedOverlay MyItemizedOverlay=null;
MyLocationOverlay MyLocationOverlay=null;
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
新建CopyData().execute();
}
}
公共类CopyData扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
Toast.makeText(getApplicationContext(),“复制已开始”,Toast.LENGTH_LONG.show();
}
@凌驾
受保护字符串doInBackground(无效…参数){
复制资产();
返回“完成”;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
Toast.makeText(getApplicationContext(),“复制完成”,Toast.LENGTH_LONG.show();
}
}
私人资产(){
InputStream in=null;
OutputStream out=null;
试一试{
in=getAssets().open(“prova.zip”);
Log.i(标记“:”+Environment.getExternalStorageDirectory());
File dir=新文件(Environment.getExternalStorageDirectory(),
“osmdroid”);
Log.i(标记“isExist:+dir.exists());
如果(!dir.exists())
dir.mkdirs();
File fileZip=新文件(dir,“prova.zip”);
Log.i(标记“isExist:+fileZip.exists());
out=新的FileOutputStream(fileZip);
复制文件(输入、输出);
in.close();
in=null;
out.flush();
out.close();
out=null;
}
捕获(IOE异常){
Log.e(“标记”,“复制资产文件失败:”+e.getMessage());
}
}
私有void copyFile(输入流输入,输出流输出)引发IOException{
字节[]缓冲区=新字节[1024];
int-read;
while((read=in.read(buffer))!=-1){
输出。写入(缓冲区,0,读取);
}
}
权限:
不要忘记向mainfest.xml文件添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
这将把你的prova.zip文件从asset文件夹复制到SD卡。那么你的问题是从assets复制到SD卡还是嵌入这个压缩的磁贴呢?你还没有解释什么不适合你。不清楚你在问什么我想把zip文件从assets复制到SD卡。我需要一个类来实现这一点,但我不知道如何实现这一点问题是我不知道必须实现哪种代码才能复制/sdcard/osmdroid中的文件
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>