3

Android でアセットにアクセスすることについて、一種の呪いに直面しています。私は NetBeans 6.9.1 を使用しており、2.3 Android 仮想デバイスでテストしています。アセットフォルダーに保存されているデータベースファイル(「database.db」)にアクセスしようとしています(プロジェクトフォルダーにも存在しなかったため、自分でディレクトリを作成する必要がありました)。解決策を探すのに3日以上失われた後、それを行うことができません。ここに私の悲劇的なプロセスの要約があります:

  • NetBeans プロジェクト ディレクトリ (「res」または「src」フォルダがある場所) に「assets」というディレクトリを作成しました。
  • 「database.db」ファイルを内部にコピーし、「sample.txt」もコピーしました。これは、人間が可能な限りすべてをテストしているため、「assets/sub」サブディレクトリにも保存しました。
  • 「SQLiteOpenHelper」から継承するクラスがあり、データベースファイルにアクセスしようとしているだけですが、それがほとんど不可能であることを確認した後、アセットフォルダーに何が含まれているかを確認しようとしています:

    public class DataBaseHelper extends SQLiteOpenHelper{
    private static String DB_PATH = "/data/data/org.me.androidbt/databases/";
    private static String DB_NAME = "database.db";
    private SQLiteDatabase myDataBase;
    private final Context myContext;
    
    public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }
    
    private void myFunction() throws IOException{
    //Firs try, explodes terribly at the first line
    InputStream is = myContext.getAssets().open("sub/sample.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line = null;
    int number = 0;
    while ((line = br.readLine()) != null) {
        // Just used to check the "line" value when debugging
        number++;
    }
    br.close();
    
    // Second try, didn't explodes
    AssetManager mngr = myContext.getResources().getAssets();
    String[] str = mngr.list("");
    // String only contains three elements: "images", "sounds" and "webkit"
    // (an there aren't such things in my assets folder)
    
    // Real try to open the DB, dies horribly and blowns up my PC
    InputStream myInput = myContext.getAssets().open(DB_NAME);
    

また、MainActivity 自体からアクセスしようとしました。

try{
        AssetManager mng = this.getAssets();
        String[] str = mng.list("");
        // The same three elements...

        // And this explodes in a nuclear toxic cloud that kills a thousand birds
        InputStream is = this.getAssets().open("sub/sample.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        int number = 0;
        while ((line = br.readLine()) != null) {
            number++;
        }
        br.close();
    }catch(Exception e){
        // Life is sad...
    }

そして今、いくつかの質問: 私が間違っていることは何ですか? 私の資産フォルダーは正しく配置されていますか? 私のファイルは正しくコピーされていますか? コードは正しく配置されていますか?

4

2 に答える 2

2

修正するには:

プロジェクト ルートに「assets」というディレクトリを作成します (AndroidManifest.xml と同じディレクトリ)。

/nbproject/project.properties で、変更します

*これらの 2 行を追加します (存在しません) *

assets.dir=資産 assets.available=true

/nbproject/build-impl.xml で、

「if=assets.available」ターゲットに次の行があります

に変更する必要があります

それだけです。すべての設定が完了し、「file:///android_asset/」がアプリケーションからアクセス可能になり、プロジェクトのアセット ディレクトリの内容が含まれているはずです。

このリンクを取得すると、うまくいきました!!...

http://blog.yetisoftware.com/2009/04/02/android-netbeans-and-the-assets-directory/

于 2011-02-07T11:33:59.033 に答える
0

i had this problem and i suggest you check couple of things:

  1. If the directory name is written in english.
  2. you have just one directory in your assets root (in this one directory you can insert couple of drectories).

For example: assets -> app -> image, assets -> app -> another dir. In this example the app dir is the only dir in the asssete root.

于 2014-09-10T08:54:23.473 に答える