Android - Could Not Open [SQL] Database Error

前端 未结 5 555
轮回少年
轮回少年 2021-01-25 11:16

I am currently making an application for the Android SDK that will allow me to open/copy a database already made outside of the application. I was having trouble getting my prog

相关标签:
5条回答
  • 2021-01-25 11:32

    Android by default compresses files with the size greater than 1MB. Older versions of Android OS has a bug, which causes compressed resource files to be read incorrectly.The easiest solution is to use resource file extensions, for example, ".mp3"

    0 讨论(0)
  • 2021-01-25 11:39

    Logcat shows the directory doesn't exist. Which is no surprise as there typically isn't a scratch directory under root on android devices.

    It's also best to avoid using absolute path names as well as you can't be certain that they'll always exist on the different devices your app may run on. The correct way to access files for your application would be to use Environment when opening a file. For example:

    File file = new File(Environment.getExternalStorageDirectory() + "/scratch/os.sqlite");
    

    This would typically exist on the sdcard or a soft linked directory designated as the "sd card" on internal memory. So this example would open a file on the sdcard under the scratch directory.

    How to get your database file on to the device or emulator is up to you. You can use the file explorer to push the file into the appropriate directory if you're using the emulator.

    0 讨论(0)
  • 2021-01-25 11:47

    Add this permission to your project

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    0 讨论(0)
  • 2021-01-25 11:52

    I responded to an answer given to me on another question and rewrote my code using two separate classes, and Adapter and a Helper, which turned out to work perfectly. I am providing the link to the code in case anyone else is having trouble with developing an SQLiteDatabase like I did.

    0 讨论(0)
  • 2021-01-25 11:58

    As far as I can remember, there is no "scratch" directory in the Android filesystem. When you call

    File file = new File("/scratch/android-sdk-linux/tools/os.sqlite");
    database = SQLiteDatabase.openOrCreateDatabase(file, null);
    

    I believe that the second statement would throw some error as the /scratch/android-sdk-linux/tools/os.sqlite filepath doesn't exist. Try calling file.mkdirs() or file.createNewFile().

    0 讨论(0)
提交回复
热议问题