尝试创建数据库并在运行此错误时发生:Failed to open database '/storage/emulated/0/database//db.sqlite'
public class G extends Application {
public static Context context;
public static String Sdk_Dir= Environment.getExternalStorageDirectory().getAbsolutePath();
public static String App_Dir=Sdk_Dir+"/database/";
public SQLiteDatabase database;
@Override
public void onCreate() {
super.onCreate();
context=getApplicationContext();
File file=new File(App_Dir);
file.mkdirs();
if (file.exists() && !file.isDirectory()) {
Toast.makeText(context, "error", Toast.LENGTH_SHORT).show();
}
database=SQLiteDatabase.openOrCreateDatabase(App_Dir+"/db.sqlite",null);
}
}
答案 0 :(得分:1)
不要忘记Manifest文件中的权限,如
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
答案 1 :(得分:0)
“App_Dir”和“db.sqlite”之间有两个斜杠,删除一个斜杠。