【问题标题】:How to save a vector object in android?如何在android中保存矢量对象?
【发布时间】:2012-06-25 12:14:21
【问题描述】:

我已经搜索了很多,但无法做到这一点。我创建了一个类“VectorEx”,扩展了“Vector”,用于存储两种方法,一种用于保存向量,另一种用于加载它。我知道我必须使用 FileOutputStream 和 openFileInput() 但我无法创建文件。我对 android 编程还是很陌生,因此感谢您提供简单的解释。

public class VectorEx extends Vector<Subject> {
public void save(String name, Context ctx) {
    try {
        File file = new File(name);
        if(!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fos = ctx.openFileOutput(name, Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(this);
        oos.close();
    }
    catch(IOException e) {
        e.printStackTrace();
    }
}

public void load(String name, Context ctx) {
    try {
        FileInputStream fis = ctx.openFileInput(name);
        ObjectInputStream ois = new ObjectInputStream(fis);
        ois.readObject();
    }
    catch(IOException e) {
        e.printStackTrace();
    }
    catch(ClassNotFoundException e) {
        e.printStackTrace();
    }
}
}

这是我上次尝试使用的代码。从我对 LogCat 的理解来看,这似乎是一个“只读文件系统”。

【问题讨论】:

  • 您是否正在测试作为大容量存储设备连接到 PC 的设备上的代码,因此无法访问 SD 卡?无论如何,本指南应该提供您所需要的:developer.android.com/guide/topics/data/data-storage.html
  • 叫什么名字?..你能告诉我们吗?
  • 我已经看过 Android Developer's Site 很多次了,但我仍然不知道我哪里出错了。一些代码会很有帮助。
  • 名字是什么?在这里打印
  • 我在调用方法时发送的“名称”变量。我想不出一种方法来使用调用该方法的对象的名称为保存文件提供唯一名称,因此我添加了一个“字符串”参数以传递给用于制作文件名的方法。

标签: android file vector save


【解决方案1】:

您的 AndroidManifest 中可能缺少这些权限之一:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

【讨论】:

  • 但我正在尝试保存到内部存储,而不是外部 SD。不过我会尝试一下。
  • 还有一个 WRITE_INTERNAL_STORAGE,两个都试试。
【解决方案2】:

我很确定您正在尝试写入根目录。您可能希望写入应用的目录或外部存储(如果有),而不是 root。

【讨论】:

  • 啊,我明白了。如何更改文件保存的目录?
【解决方案3】:
here are the vector constucts:
Vector( ) 
Vector(int size) 
Vector(int size, int incr) 
Vector(Collection c)

//Demonstrate various Vector operations. 
import java.util.*; 
class VectorDemo { 
    public static void main(String args[]) { 
        // initial size is 3, increment is 2 
        Vector v = new Vector(3, 2); 
        System.out.println("Initial size: " + v.size()); 
        System.out.println("Initial capacity: " + 
        v.capacity()); 

        v.addElement(new Integer(1)); 
        v.addElement(new Integer(2)); 
        v.addElement(new Integer(3)); 
        v.addElement(new Integer(4)); 
        System.out.println("Capacity after four additions: " + 

        v.capacity()); 
        v.addElement(new Double(5.45)); 
        System.out.println("Current capacity: " + 
        v.capacity()); 

        v.addElement(new Double(6.08)); 
        v.addElement(new Integer(7)); 
        System.out.println("Current capacity: " + 
        v.capacity()); 

        v.addElement(new Float(9.4)); 
        v.addElement(new Integer(10)); 
        System.out.println("Current capacity: " + 
        v.capacity()); 

        v.addElement(new Integer(11)); 
        v.addElement(new Integer(12)); 
        System.out.println("First element: " + 
        (Integer)v.firstElement()); 
        System.out.println("Last element: " + 
        (Integer)v.lastElement()); 

        if(v.contains(new Integer(3))) 
            System.out.println("Vector contains 3."); 
            // enumerate the elements in the vector. 
            Enumeration vEnum = v.elements(); 
            System.out.println("\\nElements in vector:"); 
            while(vEnum.hasMoreElements()) 
                System.out.print(vEnum.nextElement() + " "); 
                System.out.println(); 
            } 
        }
}



The output from this program is shown here:

Initial size: 0 
Initial capacity: 3 
Capacity after four additions: 5 
Current capacity: 5 
Current capacity: 7 
Current capacity: 9 
First element: 1 
Last element: 12 
Vector contains 3. 
Elements in vector: 
1 2 3 4 5.45 6.08 7 9.4 10 11 12

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    相关资源
    最近更新 更多