我環顧四周以找出將一般文本文件保存到Android項目中的位置,但無法找到明確的答案。當我救我「foo.txt的」文件到我的資源/原材料的文件夾中有人建議(我不得不創建原始文件夾)的文件R.java這些行得到錯誤:將文本文件保存到Android項目中的位置
public static final class raw {
public static final int 1_1=0x7f050000;
}
這是因爲我的文件在第一行包含字符串「1_1」,我希望它具有。在文件夾結構中,我應該如何讓我的文件能夠讀取它?該文件不是從Android創建的,而是由我手動創建的。
有人也請告知如何閱讀以下格式的文件?我希望能夠逐個讀取字符串和數字,並在我的Android項目中插入到java變量中。用逗號或空格分隔最好嗎?
1_1
String
Int
Int String String Int Int Float Float Int Int
Int String String Int Int Float Float Int Int
Int String String Int Int Float Float Int Int
Int String String Int Int Float Float Int Int
Int String String Int Int Float Float Int Int
Int String String Int Int Float Float Int Int
更新了更多的代碼:
package com.my.package;
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
//public class GameActivity extends FragmentActivity implements OnClickListener {
public class GameActivity extends Activity implements OnClickListener{
private ImageButton leftPauseButton;
private ImageButton rightPauseButton;
private ImageButton leftButton1;
private ImageButton leftButton2;
private ImageButton leftButton3;
private ImageButton rightButton1;
private ImageButton rightButton2;
private ImageButton rightButton3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testlayout);
TextView txtView = (TextView) (findViewById(R.id.testID_canBeRemoved));
//Did not work
//int resourceId = this.getResources().getIdentifier("com.my.package:raw/foo.txt", null, null);
//File f = new File("com.my.package:raw/foo.txt");
//Does not work - file.exists() returns a zero value
File file = new File("assets/foo.txt");
if (file.exists()){
txtView.setText("Exists");
}
else{
txtView.setText("Does not exist");
}
// InitiateUIComponents();
}
//This is for using another xml layout
private void InitiateUIComponents(){
leftPauseButton = (ImageButton) (findViewById(R.id.leftPauseButtonID));
rightPauseButton = (ImageButton) (findViewById(R.id.rightPauseButtonID));
leftButton1 = (ImageButton) (findViewById(R.id.leftMenuButton1ID));
leftButton2 = (ImageButton) (findViewById(R.id.leftMenuButton2ID));
leftButton3 = (ImageButton) (findViewById(R.id.leftMenuButton3ID));
rightButton1 = (ImageButton) (findViewById(R.id.rightMenuButton1ID));
rightButton2 = (ImageButton) (findViewById(R.id.rightMenuButton2ID));
rightButton3 = (ImageButton) (findViewById(R.id.rightMenuButton3ID));
leftPauseButton.setOnClickListener(this);
rightPauseButton.setOnClickListener(this);
leftButton1.setOnClickListener(this);
leftButton2.setOnClickListener(this);
leftButton3.setOnClickListener(this);
rightButton1.setOnClickListener(this);
rightButton2.setOnClickListener(this);
rightButton3.setOnClickListener(this);
}
//This is for using another xml layout
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.leftPauseButtonID:
Toast.makeText(this, "Left pause button clicked!", Toast.LENGTH_SHORT).show();
break;
case R.id.rightPauseButtonID:
Toast.makeText(this, "Right pause button clicked!", Toast.LENGTH_SHORT).show();
break;
case R.id.leftMenuButton1ID:
Toast.makeText(this, "Left menu button 1 clicked!", Toast.LENGTH_SHORT).show();
break;
case R.id.leftMenuButton2ID:
Toast.makeText(this, "Left menu button 2 clicked!", Toast.LENGTH_SHORT).show();
break;
case R.id.leftMenuButton3ID:
Toast.makeText(this, "Left menu button 3 clicked!", Toast.LENGTH_SHORT).show();
break;
case R.id.rightMenuButton1ID:
Toast.makeText(this, "Right menu button 1 clicked!", Toast.LENGTH_SHORT).show();
break;
case R.id.rightMenuButton2ID:
Toast.makeText(this, "Right menu button 2 clicked!", Toast.LENGTH_SHORT).show();
break;
case R.id.rightMenuButton3ID:
Toast.makeText(this, "Right menu button 3 clicked!", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
這裏是本次測試的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/testID_canBeRemoved"
android:text="Blabla"
>
</TextView>
</LinearLayout>
謝謝,這看起來像我之後。儘管如此,我遇到了文件路徑的問題。我曾嘗試使用「file:///android_asset/foo.txt」,但這不起作用。我通過在線搜索嘗試了幾種方法,但似乎沒有任何方法可行。我錯過了什麼? – 2012-03-12 17:34:03
僅供參考這些工作都不工作,它們都強制IOException: InputStream inStream = this.getAssets()。open(「foo。(); – 2012-03-12 17:52:43
這些例子也不起作用,file.exsists()返回零: File file =新文件(「file:///android_asset/foo.txt」); File file = new File(「foo.txt」); – 2012-03-12 18:41:06