我的Android之旅(十七)---raw和assets目录--数据的存取

    xiaoxiao2026-10-07  0

    

    raw是Resources (res)的子目录,Android会自动的为这目录中的所有资源文件生成一个ID,这个ID会被存储在R类当中,作为一个文件的引用。这意味着这个资源 文件可以很容易的被Android的类和方法访问到,甚至在Android XML文件中你也可以@raw/的形式引用到它。

    assets目录更像一个附录类型的目录,Android不会为这个目录中的文件生成ID并保存在R类当中,因此它与 Android中的一些类和方法兼容度更低。

    raw和assets的相同点:  1.两者目录下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制。 raw和assets的不同点:   1.raw中的文件会被映射到R.java文件中,访问的时候直接使用资源ID即R.id.filename;assets文件夹下的文件不会被映射到R.java中,访问的时候需要AssetManager类。   2.raw不可以有目录结构,而assets则可以有目录结构,也就是assets目录下可以再建立文件夹

    二者读取文件内容的方式也不同:

      1.读取raw下的文件资源,通过以下方式获取输入流来进行写操作   InputStream is = getResources().openRawResource(R.id.filename);   2.(1)读取assets下的文件资源,通过以下方式获取输入流来进行写操作   AssetManager am = null;   am = getAssets();   InputStream is = am.open("filename");

    下面的例子就体现了二者在读取文件资源时的不同之处;

    首先要建立raw文件以及assets目录,并分别在二者中新建File文件:

               

    并在两个文件中任意输入一些数据,作为读取的资源。

    .xml文件中的代码:

    package com.jerehedu.jereduch10; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class ReadRawAndAssetsActivity extends AppCompatActivity { private Button raw,assets; private TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_read_raw_and_assets); raw= (Button) findViewById(R.id.raw); assets= (Button) findViewById(R.id.assets); show= (TextView) findViewById(R.id.show); raw.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { show.setText(readRaw()); } }); assets.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { show.setText(readAssets()); } }); } //读取Assets目录 public String readAssets(){ StringBuilder sbd=new StringBuilder(); InputStream is=null; BufferedReader reader=null; try { is= getResources().getAssets().open("setting"); reader=new BufferedReader(new InputStreamReader(is)); String row=""; while ((row=reader.readLine())!=null){ sbd.append(row); sbd.append("\n"); } } catch (IOException e) { e.printStackTrace(); }finally { if (reader!=null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return sbd.toString(); } //读取raw文件夹 public String readRaw(){ StringBuilder sbd=new StringBuilder(); InputStream is=null; BufferedReader reader = null; is=getResources().openRawResource(R.raw.setting1); reader=new BufferedReader(new InputStreamReader(is)); String row = ""; try { while ((row=reader.readLine())!=null){ sbd.append(row); sbd.append("\n"); } } catch (IOException e) { e.printStackTrace(); }finally { if (reader!=null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return sbd.toString(); } }

    二者的区别在于:

    获取数据的方法不同,其余差不多

    读取Assets目录 is= getResources().getAssets().open("setting"); 读取raw文件夹 is=getResources().openRawResource(R.raw.setting1);

    布局中的代码:

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.jerehedu.jereduch10.ReadRawAndAssetsActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/raw" android:text="读取raw" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/assets" android:text="读取assets" android:layout_marginEnd="49dp" android:layout_alignParentTop="true" android:layout_alignEnd="@+id/show" /> <TextView android:layout_width="match_parent" android:layout_height="300dp" android:id="@+id/show" android:layout_centerVertical="true" android:layout_alignParentStart="true" /> </RelativeLayout>

    运行结果如下:

    点击raw按钮:

    点击ASSETS按钮:

    在学的过程中一定要理解二者的区别。

    转载请注明原文地址: https://ju.6miu.com/read-1312705.html
    最新回复(0)