【问题标题】:How use a xml file in android from resources如何从资源中使用android中的xml文件
【发布时间】:2016-12-14 15:16:40
【问题描述】:

我正在尝试使用需要解析的 xml 文件,但只有手动将此文件复制到路径上的模拟器(API 级别 24)中才能使用它:/mnt/sdcard

我需要这个文件到我的应用程序的.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 文件吗?

所以现在我的 Activity 看起来像这样:

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

【问题讨论】:

    标签: android xml xml-parsing


    【解决方案1】:

    您可以使用assets 文件夹并将其放在那里。要访问File,请像这样使用它

     InputStream is = getResources().getAssets().open("yourFile.xml");
    

    【讨论】:

    • 我收到了一个InputStream 对象,我在我的方法中使用了一个文件对象:public static Metro miMetro = parser.parsearMetro(xml); 而且我不能使用静态引用,我想使用这个 Metro 对象,那些行在其他类中创建。
    • @MiguelMG 在这种情况下,您必须在 sdcard 路径中创建一个新的 File,例如something like this
    • 但这不会出现在.apk 中,不是吗?
    • @MiguelMG 它将在 apk 中。您基本上将使用 InputStream 创建一个新文件,因此您有一个FileObject。除此之外,您的 Parser 可能还是使用了 Inputstream。
    • 在此之后出现错误,我已编辑问题
    猜你喜欢
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多