无法打开数据库 - Android

时间:2017-08-28 12:11:24

标签: android android-sqlite android-permissions android-assets

我正在使用API 17 (4.2.2)测试我的应用程序以获得荣誉,但让我震惊:

 E/asdf: checkDataBase-->android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.Error: Error copying database --> java.io.FileNotFoundException: /storage/sdcard1/Android/data/ir.jmostafa.appportalmostafa/appPortal/AppPortalDB.sqlite: open failed: EACCES (Permission denied)
    at ir.jmostafa.appportalmostafa.db.DBS.CreateandOpenDataBase(DBS.java:89)
    at ir.jmostafa.appportalmostafa.start.StartActivity.createDatabase(StartActivity.java:296)
    at ir.jmostafa.appportalmostafa.start.StartActivity.init(StartActivity.java:227)
    at ir.jmostafa.appportalmostafa.start.StartActivity.onCreate(StartActivity.java:143)
    at android.app.Activity.performCreate(Activity.java:5179)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2336)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
    at android.app.ActivityThread.access$600(ActivityThread.java:169)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1388)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5433)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:924)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
    at dalvik.system.NativeStart.main(Native Method)

而贝娄是我的DBS.java

public class DBS extends SQLiteOpenHelper {
    public static final String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
    public static final String DIR_DATABASE = DIR_SDCARD + "/Android/data/";
    private static String DB_NAME = "AppPortalDB.sqlite";
    private final Context myContext;
    public static String PACKAGE_NAME;

    private void copyDataBase() throws IOException {
        // Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DIR_DATABASE + PACKAGE_NAME + "/appPortal/" + DB_NAME;

        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        try {
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
        } catch (IOException e) {
            Log.e("Copy", e.toString());
        }
        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            checkDB = SQLiteDatabase.openDatabase(DIR_DATABASE + PACKAGE_NAME + "/appPortal/" + DB_NAME, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);

        } catch (SQLiteException e) {
            Log.e("asdf", "checkDataBase-->" + e.toString());
        }
        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

    @Override
    public synchronized SQLiteDatabase getReadableDatabase() {
        return super.getReadableDatabase();
    }

    public DBS(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    public void CreateandOpenDataBase() throws IOException {
        boolean dbExist = false;
        try {
            dbExist = checkDataBase();
        } catch (Exception e) {
            Log.i("ERROR", "ERROR in DBS Class");
        }
        if (dbExist) {
        } else {
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database --> " + e.toString());
            }
        }
    }

    public SQLiteDatabase openDataBase() throws SQLException {
        return SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + PACKAGE_NAME + "/appPortal/" + DB_NAME, null);
    }

    public boolean CreateFile() {
        File fileExist = new File(DIR_DATABASE + PACKAGE_NAME + "/appPortal");
        if (!fileExist.exists() && !fileExist.isDirectory()) {
            File file = new File(DIR_DATABASE + PACKAGE_NAME + "/appPortal");
            file.mkdirs();
            return true;
        } else {
            return false;
        }
    }

    public void GetPackageName(String res) {
        PACKAGE_NAME = res;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }
} 

我在我的清单中添加了以下许可:

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

注意:但是当我使用此标记在其他设备上测试应用程序并为应用程序建模时,该工作正常。

1 个答案:

答案 0 :(得分:0)

问题在于您的文件java.io.FileNotFoundException: /storage/sdcard1/Android/data/ir.jmostafa.appportalmostafa/appPortal/AppPortalDB.sqlite确保路径正确且文件在那里