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は、パスが正しいことを確認し、ファイルが存在するファイルである