在android内部存储器中添加多个数据

在android内部存储器中添加多个数据,android,storage,internal,Android,Storage,Internal,我想做的是在Android内部存储器中保存多个数据,我想存储不同的产品详细信息,包括产品图像、产品名称、简短描述和价格,正如我所说的,我想保存大约10种产品的所有这些信息。 我的问题是如何保存这些数据。首先创建一个项目,并在项目中添加以下代码 main.xml <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android

我想做的是在Android内部存储器中保存多个数据,我想存储不同的产品详细信息,包括产品图像、产品名称、简短描述和价格,正如我所说的,我想保存大约10种产品的所有这些信息。
我的问题是如何保存这些数据。

首先创建一个项目,并在项目中添加以下代码

main.xml

<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Please enter some text”
/>
<EditText
android:id=”@+id/txtText1”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
<Button
android:id=”@+id/btnSave”
android:text=”Save”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
<Button
android:id=”@+id/btnLoad”
android:text=”Load”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
</LinearLayout>
上面是演示的代码


使用osw.write(“required text”)在上面的代码中添加所需的数据。

我认为最好的方法是使用SQLite数据库

这里有一个很好的教程:


您也可以随时在internet上搜索其他人。

如果以上答案对您有帮助,请接受答案并向上投票answer@user3054226谢谢你的回复,但是我在网上发现了同样的东西,它不能帮助我解决我的问题,我想要的是找到一种方法来存储我提到的所有数据,比如在内部存储器中保存图像和其他三个字符串。不建议将图像保存在SQLite中,但无论如何,感谢您的回复@Hbibna
package net.learn2develop.Files;
import android.app.Activity;
import android.view.View;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText textBox;
private static final int READ_BLOCK_SIZE = 100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textBox = (EditText) findViewById(R.id.txtText1);
Button saveBtn = (Button) findViewById(R.id.btnSave);
Button loadBtn = (Button) findViewById(R.id.btnLoad);
saveBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String str = textBox.getText().toString();
try
{
FileOutputStream fOut =
openFileOutput(“textfile.txt”,
MODE_WORLD_READABLE);
OutputStreamWriter osw = new
OutputStreamWriter(fOut);
//---write the string to the file---
osw.write(str);
osw.flush();
osw.close();
//---display file saved message---
Toast.makeText(getBaseContext(),
“File saved successfully!”,
Toast.LENGTH_SHORT).show();
//---clears the EditText---
textBox.setText(“”);
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
});
loadBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try
{
FileInputStream fIn =
openFileInput(“textfile.txt”);
InputStreamReader isr = new
InputStreamReader(fIn);
char[] inputBuffer = new char[READ_BLOCK_SIZE];
String s = “”;
int charRead;
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0,
charRead);
s += readString;
inputBuffer = new char[READ_BLOCK_SIZE];
}
//---set the EditText to the text that has been
// read---
textBox.setText(s);
Toast.makeText(getBaseContext(),
“File loaded successfully!”,
Toast.LENGTH_SHORT).show();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
});
}
}