首次运行时未加载数据库 API 26



我在资产文件夹中创建了一个带有数据库的应用程序。我写了一个代码将数据库复制到SD卡,当然对于Android 6 ,它需要运行时间许可。我的问题:在授予许可数据库后的第一次运行时,没有加载,但是在第二次运行时,没有问题。请帮助我解决这个问题。

更新:解决问题!现在,我对喜欢的部分有问题。当我将某些内容添加到最爱中时,它无法更新,我必须重新启动应用程序,并且每次运行数据都将多次显示。

这是我的代码:

package farmani.com.essentialwordsforielts.mainPage;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import farmani.com.essentialwordsforielts.R;
import farmani.com.essentialwordsforielts.search.ActivitySearch;
public class MainActivity extends AppCompatActivity {
public static Context context;
public static ArrayList<Structure> list = new ArrayList<>();
public static ArrayList<Structure> favorite = new ArrayList<>();
DrawerLayout drawerLayout;
NavigationView navigationView;
ImageView hamburger;
SQLiteDatabase database;
String destPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navigation_activity_main);
    if (Build.VERSION.SDK_INT >= 23) {

        if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE) !=
                PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this
                    , new String[]{Manifest.permission.READ_EXTERNAL_STORAGE
                            , Manifest.permission.WRITE_EXTERNAL_STORAGE}
                    , 1);
        } else if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
                PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this
                    , new String[]{Manifest.permission.READ_EXTERNAL_STORAGE
                            , Manifest.permission.WRITE_EXTERNAL_STORAGE}
                    , 1);
        } else {
            setupDB();
            selectList();
            selectFavorite();

            Toast.makeText(MainActivity.this, "You grandet earlier", 
Toast.LENGTH_LONG).show();
        }

    }
    if (!favorite.isEmpty()){
        favorite.clear();
        selectFavorite();
    } else if (!list.isEmpty()){
        list.clear();
        selectList();
    }
    context = getApplicationContext();
    setTabOption();
    drawerLayout = findViewById(R.id.navigation_drawer);
    navigationView = findViewById(R.id.navigation_view);
    hamburger = findViewById(R.id.hamburger);
    hamburger.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            drawerLayout.openDrawer(Gravity.START);
        }
    });
    navigationView.setNavigationItemSelectedListener(new 
   NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.exit) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                        MainActivity.this);
                alertDialog.setTitle(R.string.exit);
                alertDialog.setMessage(R.string.exit_ask);
                alertDialog.setCancelable(false);
                alertDialog.setPositiveButton(R.string.yes,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int
                                    which) {
                                finish();
                            }
                        });
                alertDialog.setNegativeButton(R.string.no,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int
                                    which) {
                                dialog.cancel();
                            }
                        });
                alertDialog.show();
            }
            if (id == R.id.search) {
                Intent intent = new Intent(MainActivity.this, 
    ActivitySearch.class);
                MainActivity.this.startActivity(intent);
            }
            return true;
        }
    });

    }
   @Override
   public void onRequestPermissionsResult(int requestCode, @NonNull String[] 
  permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case 1: {
            if (grantResults.length >= 2 && grantResults[0] ==
                    PackageManager.PERMISSION_GRANTED && grantResults[1] ==
                    PackageManager.PERMISSION_GRANTED) {

                Toast.makeText(MainActivity.this, "Access granted",
                        Toast.LENGTH_LONG).show();
            }
        }
    }
  }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(Gravity.START)) {
        drawerLayout.closeDrawer(Gravity.START);
        } else {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                MainActivity.this);
        alertDialog.setTitle(R.string.exit);
        alertDialog.setMessage(R.string.exit_ask);
        alertDialog.setCancelable(false);
        alertDialog.setPositiveButton(R.string.yes,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
        alertDialog.setNegativeButton(R.string.no,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        alertDialog.show();
    }
}

    private void setTabOption() {
       ViewPager viewPager = findViewById(R.id.viewpager);
       viewPager.setAdapter(new AdapterFragment(getSupportFragmentManager(),
            context));
       TabLayout tabStrip = findViewById(R.id.tabs);
       tabStrip.setupWithViewPager(viewPager);
    }
    private void setupDB() {
    try {
        destPath =
                Environment.getExternalStorageDirectory().getAbsolutePath() 
    + "/ielts/";
        File file = new File(destPath);
        if (!file.exists()) {
            file.mkdirs();
            file.createNewFile();
            CopyDB(getBaseContext().getAssets().open("md_book.db"),
                    new FileOutputStream(destPath + "/md_book.db"));
        }
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}
    @Override
    protected void onResume() {
    super.onResume();
    if (!favorite.isEmpty()){
        favorite.clear();
        selectFavorite();
    } else if (!list.isEmpty()){
        list.clear();
        selectList();
    }
}
private void CopyDB(InputStream inputStream, OutputStream outputStream)
        throws IOException {
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
        outputStream.write(buffer, 0, length);
    }
    inputStream.close();
    outputStream.close();
}
private void selectFavorite() {
    database = SQLiteDatabase.openOrCreateDatabase(destPath + "/md_book.db",
            null);
    Cursor cursor = database.rawQuery("SELECT * FROM main WHERE fav = 1",
            null);
    while (cursor.moveToNext()) {
        String word = cursor.getString(cursor.getColumnIndex("word"));
        String definition =
                cursor.getString(cursor.getColumnIndex("definition"));
        String trans = cursor.getString(cursor.getColumnIndex("trans"));
        String img = cursor.getString(cursor.getColumnIndex("img"));
        int id = cursor.getInt(cursor.getColumnIndex("id"));
        Structure struct = new Structure(word, definition, trans, img, id);
        struct.setWord(word);
        struct.setDefinition(definition);
        struct.setTrans(trans);
        struct.setImg(img);
        struct.setId(id);
        favorite.add(struct);
    }
}
    private void selectList() {
    database = SQLiteDatabase.openOrCreateDatabase(destPath + "/md_book.db",
            null);
    Cursor cursor = database.rawQuery("SELECT * FROM main", null);
    while (cursor.moveToNext()) {
        String word = cursor.getString(cursor.getColumnIndex("word"));
        String definition =
                cursor.getString(cursor.getColumnIndex("definition"));
        String trans = cursor.getString(cursor.getColumnIndex("trans"));
        String img = cursor.getString(cursor.getColumnIndex("img"));
        int id = cursor.getInt(cursor.getColumnIndex("id"));
        Structure struct = new Structure(word, definition, trans, img, id);
        struct.setWord(word);
        struct.setDefinition(definition);
        struct.setTrans(trans);
        struct.setImg(img);
        struct.setId(id);
        list.add(struct);
    }
}
}

您刚刚在第一次授予权限后(即onRequestPermissionsResult内(显示了烤面包。您将需要放置代码以在其中执行必要的操作。

最新更新