2011-08-04 2 views
0

이 인벤토리 인벤토리를 내부 저장소에 저장하려고합니다. 클래스 자체에서 메소드를 저장하고 가져 왔습니다. 시도하고 save 메서드를 호출 할 때 예외로 끝납니다. 나는 로그 캣에 예외 메시지 쓰기가 있고, 여기에 내가 가진 무엇 :개체를 쓸 수 없습니다. 읽기 전용 파일 시스템

08-04 02 : 32 : 23.690 : VERBOSE/알렉스 (278)/시험 (읽기 전용 파일 시스템)

파일/테스트는 "읽기 전용 파일 시스템"입니다,하지만 난 매니페스트 파일에 외부 저장 장치를 쓰기 허용했다 :

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

다음은 재고 클래스입니다. 마지막 두 메서드는 저장 및 읽기 메서드입니다.

package com.androidbook.inventoryproject; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 
import java.util.ArrayList; 

import android.util.Log; 

public class Inventory implements Serializable { 
    private static final long serialVersionUID = 1L; 
    int numIngred;; 
    Ingredient[] ingredients; 
    ArrayList ingred = new ArrayList<Ingredient>(); 

    public Inventory() { 
     numIngred = 0; 
     ingredients = new Ingredient[numIngred]; 
    } 

    public int getNumIngred() { 

     return numIngred; 
    } 

    public String getIngredientName(int n) { 
     return ((Ingredient)ingred.get(n)).getName(); 

    } 

    public Ingredient[] getIngredients() { 
     return ingredients; 
    } 

    public Ingredient getIngredient(int n) { 
     return (Ingredient)ingred.get(n); 
    } 

    public void addIngredient(String iname) { 
     numIngred++; 
     ingred.add(new Ingredient(iname)); 
    } 

    public boolean saveInventory(Inventory inv) { 
     File suspend_f = new File("test"); 
     FileOutputStream fos = null; 
     ObjectOutputStream oos = null; 
     boolean   keep = true; 
     try { 
      fos = new FileOutputStream(suspend_f); 
      oos = new ObjectOutputStream(fos); 
      oos.writeObject(inv); 
     } 
     catch (Exception e) { 
      keep = false; 
      Log.v("alex", "" + e.getMessage()); 

     } 
     finally { 
      try { 
       if (oos != null) oos.close(); 
       if (fos != null) fos.close(); 
       if (keep == false) suspend_f.delete(); 
      } 
      catch (Exception e) { /* do nothing */ } 
     } 
     return keep; 
    } 

    public Inventory getInventory() { 
     File suspend_f = new File("test"); 
     Inventory inven = null; 
     FileInputStream fis = null; 
     ObjectInputStream ois = null; 

     try{ 
      fis = new FileInputStream(suspend_f); 
      ois = new ObjectInputStream(fis); 
      inven = (Inventory)ois.readObject(); 
     } 
     catch (Exception e) { 
      String mess = e.getMessage(); 

     } 
     finally { 
      try { 
       if (fis != null) 
        fis.close(); 
       if (ois != null) 
        ois.close(); 
      } 
      catch (Exception e) { } 
     } 
     return inven; 
    } 
} 

답변

2

WRITE_EXTERNAL_STORAGE 파일 시스템 루트가 아닌 SD 카드에 쓸 수 있습니다. 시도해보십시오 :

File suspend_f = new File(Environment.getExternalStorageDirectory(), "test"); 

이렇게하면 쓰기 가능한 외부 폴더로 이동하는지 확인합니다.

편집 : SD 카드가 사용 가능하고 쓰기 가능한지 확인하기 위해해야 ​​할 일이 많이 있습니다. 가용성을 확인하여 파일 액세스를 강력하게 만드는 방법을 보려면 specs을 읽어보십시오.

+0

아, 잠깐, 실제로 파일 시스템 루트에 쓰고 싶습니다. 어떻게해야합니까? 감사! –

+0

할 수 없습니다. 기본적으로 앱의 개인 저장 공간과 외부 저장소에만 쓸 수 있습니다. –

+0

@Chris가 옳습니다. 아마도 루트 파티션의 읽기 전용 특성을 수정했다면 수정할 수는 없지만 수정되지 않은 안드로이드 장치에서는 결코 할 수 없을 것입니다. 무엇을 할 수 있겠는가? 구체적으로 루트에 글을 써야합니까? – Femi

관련 문제