![](/img/trans.png)
[英]How to get an inputStream from an xml file at resources xml folder in Android
[英]How use a xml file in android from resources
我正在嘗試使用我需要解析的xml文件,但是只有在將該文件手動復制到仿真器(API級別24)的以下路徑中時,才能使用該文件: /mnt/sdcard
我需要將此文件放入我的App的.apk
中,所以我試圖從我的項目的/ res文件夾中使用它,因為我知道這個文件最終也將包含在.apk
。
這就是我現在從sdcard
使用此文件的方式:
public static File xml = new File("/mnt/sdcard/metro.xml");
public static Parser parser = new Parser();
public static Metro miMetro = parser.parsearMetro(xml);
但我想更改此文件的位置,因為我希望它位於App內。
如果我嘗試:
public static File xml = new File("/res/xml/metro.xml");
應用程式說找不到該檔案。
這是AndroidManifest.xml
應用權限的樣子:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
有人知道我如何在不更改我的方法的情況下將該文件包含到最終的.apk
,並在內部使用它而不在每個設備中復制xml文件嗎?
所以現在我的活動看起來像這樣:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.miguel.metromadappcesible.code.Metro;
import com.miguel.metromadappcesible.code.Parser;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class IndexActivity extends AppCompatActivity {
public static Parser parser = new Parser();
public static Metro miMetro;
public IndexActivity() throws IOException {
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
try {
this.crearFicheroXML(getAssets().open("metro.xml"));
} catch (IOException e) {
e.printStackTrace();
}
File xml = new File ("/mnt/sdcard/metroMadrid.xml");
miMetro = parser.parsearMetro(xml);
}
public void crearFicheroXML (InputStream inputStream){
OutputStream outputStream = null;
try {
// read this file into InputStream
inputStream = getResources().getAssets().open("metro.xml");
// write the inputStream to a FileOutputStream
outputStream =
new FileOutputStream(new File("/mnt/sdcard/metroMadrid.xml"));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
// outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我從控制台收到此錯誤: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
您可以使用assets
文件夾並將其放在那里。 要訪問File
,請像這樣使用
InputStream is = getResources().getAssets().open("yourFile.xml");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.