首页 > 解决方案 > 无法从 SD 卡加载文件

问题描述

我有一个在 assets 文件夹中有数据库的应用程序。但我想从我的 SD 卡而不是资产中获取数据库,因为我是新手,所以我做了这些步骤:

  1. 我下载了新数据库并将其放在自定义文件夹中。
  2. 我将 DB_PATH 从

DB_PATH = "/data/data/com.test.project/databases/";

DB_PATH = Environment.getExternalStorageDirectory() + "/my_custom_folder/";

但是当我尝试访问它时,应用程序崩溃了,并且该数据库已从我的手机中删除,有什么帮助吗?

这是加载它的代码:

public class book_DB_Helper extends SQLiteOpenHelper {

public final static int DB_VERSION = 1;
private final static String DB_NAME = "my_book";
public static SQLiteDatabase db;

       //// --- the old PATH 'from assets folder
   //private static String DB_PATH = "/data/data/com.test.project/databases/";

      //// --- my new path in the sdcard   
private static String DB_PATH = Environment.getExternalStorageDirectory() + "/my_folder/";

private final Context context;

private final String TABLE_NAME = "tbl_recipes";
private final String ID = "id";
private final String RECIPE_NAME = "recipe_name";
private final String IMAGE_PREVIEW = "image_preview";
private final String COOK_TIME = "cook_time";

public my_book_DB_Helper(Context context) {

    super(context, DB_NAME, null, DB_VERSION);
    this.context = context;
}

public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();
    SQLiteDatabase db_Read = null;


    if (dbExist) {
        //do nothing - database already exist
        deleteDataBase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    } else {
        db_Read = this.getReadableDatabase();
        db_Read.close();

        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }

}

private void deleteDataBase() {
    File dbFile = new File(DB_PATH + DB_NAME);

    dbFile.delete();
}

private boolean checkDataBase() {

    File dbFile = new File(DB_PATH + DB_NAME);

    return dbFile.exists();

}


private void copyDataBase() throws IOException {

    InputStream myInput = context.getAssets().open(DB_NAME);

    String outFileName = DB_PATH + DB_NAME;

    OutputStream myOutput = new FileOutputStream(outFileName);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}

@Override
public void close() {
    db.close();
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


public ArrayList<ArrayList<Object>> getAllData(String RecipeNameKeyword) {
    ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();

    Cursor cursor = null;

    if (RecipeNameKeyword.equals("")) {
        try {
            cursor = db.query(
                    TABLE_NAME,
                    new String[]{ID, RECIPE_NAME, IMAGE_PREVIEW, COOK_TIME},
                    null, null, null, null, null);
            cursor.moveToFirst();

            if (!cursor.isAfterLast()) {
                do {
                    ArrayList<Object> dataList = new ArrayList<Object>();

                    dataList.add(cursor.getLong(0));
                    dataList.add(cursor.getString(1));
                    dataList.add(cursor.getString(2));
                    dataList.add(cursor.getString(3));

                    dataArrays.add(dataList);
                }

                while (cursor.moveToNext());
            }
            cursor.close();
        } catch (SQLException e) {
            Log.e("DB Error", e.toString());
            e.printStackTrace();
        }
    } else {
        try {
            cursor = db.query(
                    TABLE_NAME,
                    new String[]{ID, RECIPE_NAME, IMAGE_PREVIEW, COOK_TIME},
                    RECIPE_NAME + " LIKE '%" + RecipeNameKeyword + "%'",
                    null, null, null, null);
            cursor.moveToFirst();

            if (!cursor.isAfterLast()) {
                do {
                    ArrayList<Object> dataList = new ArrayList<Object>();

                    dataList.add(cursor.getLong(0));
                    dataList.add(cursor.getString(1));
                    dataList.add(cursor.getString(2));
                    dataList.add(cursor.getString(3));

                    dataArrays.add(dataList);
                }

                while (cursor.moveToNext());
            }
            cursor.close();
        } catch (SQLException e) {
            Log.e("DB Error", e.toString());
            e.printStackTrace();
        }
    }
    return dataArrays;
}


public ArrayList<Object> getDetail(long id) {

    ArrayList<Object> rowArray = new ArrayList<Object>();
    Cursor cursor;

    try {
        String SUMMARY = "summary";
        String PREP_TIME = "prepare_time";
        String SERVES = "serves";
        String INGREDIENTS = "ingredients";
        String DIRECTIONS = "directions";
        cursor = db.query(
                TABLE_NAME,
                new String[]{RECIPE_NAME, IMAGE_PREVIEW, COOK_TIME},
                ID + "=" + id,
                null, null, null, null, null);

        cursor.moveToFirst();

        if (!cursor.isAfterLast()) {
            do {
                rowArray.add(cursor.getString(0));
                rowArray.add(cursor.getString(1));
                rowArray.add(cursor.getString(2));
                rowArray.add(cursor.getString(3));
                rowArray.add(cursor.getString(4));
                rowArray.add(cursor.getString(5));
                rowArray.add(cursor.getString(6));
                rowArray.add(cursor.getString(7));
            }
            while (cursor.moveToNext());
        }

        cursor.close();
    } catch (SQLException e) {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();
    }

    return rowArray;
}

}

标签: javaandroidsqliteandroid-sdcardandroid-external-storage

解决方案


推荐阅读