Failed to open database - Android

869 Views Asked by At

I am testing my app on honor with API 17 (4.2.2) but get me bellow crash :

 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)

And bellow is my 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) {

    }
} 

And I added bellow permission in my manifest :

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

Notice : But when I test the application on other device with this mark and model the application good work .

1

There are 1 best solutions below

0
On

The problem is with your file java.io.FileNotFoundException: /storage/sdcard1/Android/data/ir.jmostafa.appportalmostafa/appPortal/AppPortalDB.sqlite make sure the path is correct and the file is there