无法从SD卡加载文件

时间:2018-08-06 00:24:42

标签: java android sqlite android-sdcard android-external-storage

我有一个在资产文件夹内具有数据库的应用程序。 但是我想从我的sdcard中获取数据库,而不是从资产中获取数据库,因为我对此并不陌生,我做了以下步骤:

  1. 我下载了新的数据库并将其放在自定义文件夹中。
  2. 我从
  3. 更改了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;
}

}

0 个答案:

没有答案