【问题标题】:add multiple data in internal storage android在内部存储android中添加多个数据
【发布时间】:2014-03-16 12:05:27
【问题描述】:

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

【问题讨论】:

    标签: android storage internal


    【解决方案1】:

    首先创建一个项目,在项目中添加如下代码。我是添加.java和.xml代码

    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>
    

    mainActivity.Java

    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();
    }
    }
    });
    }
    }
    

    以上是演示代码。

    使用 osw.write("required text") 在上述代码中添加所需数据

    【讨论】:

    • 如果以上答案对您有帮助,请接受答案并为答案投票
    • @user3054226 关于我的回答
    • 感谢您的回复,但我在互联网上发现了同样的事情,无法帮助我解决我的问题,我正在寻找一种方法来存储我提到的所有数据比如在内存中保存图像和其他三个字符串
    【解决方案2】:

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

    这里有一个很好的教程:http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

    您可以随时在互联网上搜索其他人。

    【讨论】:

    • 不建议在SQLite中保存图片,不过还是谢谢@Hbibna的回复
    猜你喜欢
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 2015-08-29
    • 2015-06-19
    • 1970-01-01
    相关资源
    最近更新 更多