2017-08-28 113 views
0

我測試我的榮幸應用與API 17 (4.2.2)但讓我吼叫崩潰:無法打開數據庫 - Android電子

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" /> 

注意:但是當我測試其他應用程序時r器件具有這種標記和模型應用的良好工作。

+0

請檢查這個問題,https://stackoverflow.com/a/23529376/5255006,可它幫助 –

回答

0

問題是與您的文件java.io.FileNotFoundException: /storage/sdcard1/Android/data/ir.jmostafa.appportalmostafa/appPortal/AppPortalDB.sqlite確保路徑是正確的,該文件是存在的