从外部存储打开数据库(Sqlite open helper)

时间:2017-05-25 10:55:47

标签: java android sqlite android-sqlite android-sdcard

我正在尝试打开以前从我的Google云端硬盘下载的数据库。

然而,每当我尝试打开它时,我都会遇到这个例外:

FATAL EXCEPTION: main
Process: com.example.paolomazza.final_database_downloader, PID: 31663
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.paolomazza.final_database_downloader/com.example.paolomazza.final_database_downloader.MainActivity}: com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases//storage/emulated/0/Android/data/com.example.paolomazza.final_database_downloader/files//storage/emulated/0/Prova.db file (or .zip, .gz archive) in assets, or target folder not writable
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases//storage/emulated/0/Android/data/com.example.paolomazza.final_database_downloader/files//storage/emulated/0/Prova.db file (or .zip, .gz archive) in assets, or target folder not writable
    at android.content.res.AssetManager.openAsset(Native Method)
    at android.content.res.AssetManager.open(AssetManager.java:347)
    at android.content.res.AssetManager.open(AssetManager.java:321)
    at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.copyDatabaseFromAssets(SQLiteAssetHelper.java:436)
    at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.createOrOpenDatabase(SQLiteAssetHelper.java:400)
    at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:176)
    at com.example.paolomazza.final_database_downloader.DatabaseAccess.open(DatabaseAccess.java:46)
    at com.example.paolomazza.final_database_downloader.MainActivity.onCreate(MainActivity.java:60) 
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

我创建了三个类,两个用于数据库,一个用于主要活动。 有人可以帮我找到我收到此错误的原因吗?有人可以为它发布解决方案吗?非常感谢你。

MainActivity

public class MainActivity extends AppCompatActivity {

    ListView listview;
    ProgressDialog mProgressDialog;
    DatabaseAccess databaseAccess;


    private static final int REQUEST_EXTERNAL_STORAGE = 1;

    private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        verifyStoragePermissions(this);
        setContentView(R.layout.activity_main);

        try {
            // Download Image from URL
            InputStream input = new java.net.URL("https://drive.google.com/uc?export=download&id=0B012r3FpS-3iRFk3SU5HbDRtY1U").openStream();
            // Decode Bitmap
            OutputStream outputStream = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Prova.db");
            IOUtils.copy(input, outputStream);
        } catch (Exception e) {

        }

        databaseAccess = DatabaseAccess.getInstance(this);
        databaseAccess.open();

        ArrayList<String> names = databaseAccess.getAllnames();
        listview = (ListView)findViewById(R.id.List);
        ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names);
        listview.setAdapter(countryAdapter);
    }

    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
            );
        }
    }

这是给我错误的类

public class DatabaseOpenHelper extends SQLiteAssetHelper  {

    private static final String DATABASE_NAME = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Prova.db";
    private static final int DATABASE_VERSION = 1;


    public DatabaseOpenHelper(Context context){
        super(context, context.getExternalFilesDir(null).getAbsolutePath() + "/" + DATABASE_NAME , null, DATABASE_VERSION);
    }
}

0 个答案:

没有答案