Open Database for ReadWirte not possible on specific device - android

I simply try to open a database in READWRITE Mode. I get the following error: "not an error (code 0): Could not open the database in read/write mode." When opening the DB the same way but READONLY is works. On my 3 devices this is not a problem opening in READWRITE, but 2 users reported that following error.
the db exists on the users filesystem . I check this with file.exists() -> ok.
the db-file is readwrite able on the users device. I check this with file.canWrite() -> ok.
the dbfile is stored under :
StoragePath : /mnt/extSdCard/mypath/mydb.db
New Info on 09.03.2014 : It seems to be only a problem in KitKat 4.4.2. Since Users have updated to 4.4.2 they get this problem.
My Code:
public void onCreate(Bundle savedInstanceState) {
connectWriter(); // throws exception below.
public void connectWriter() {
chronica_connection_readwrite = SQLiteDatabase.openDatabase("MyPath", null, SQLiteDatabase.OPEN_READWRITE);
//chronica_connection_read.enableWriteAheadLogging();
}
Exception Report:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: android.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
at android.app.ActivityThread.access$800(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:342)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:232)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:515)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:207)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:891)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:859)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
at myclass.connectWriter(ChronicBrowser.java:14286)
at myclass.LoadModule(ChronicBrowser.java:10792)
at myclass.onCreate(ChronicBrowser.java:761)
at android.app.Activity.performCreate(Activity.java:5389)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
************ DEVICE INFORMATION ***********
Brand: samsung
Device: hlte
Model: SM-N9005
Id: KOT49H
Product: hltexx
************ FIRMWARE ************
SDK: 19
Release: 4.4.2
Incremental: N9005XXUENB7

Looks like that this is not bug, but "feature".
Google's KitKat Blocks Some Access to Micro SD Cards
It's really sad, but looks like true

I'll preface this by saying you should really be storing SQLite databases on internal storage, where you have the benefit of ext4 journaling to help with data recovery. There is also much better security on internal storage, since you don't have to worry about malicious apps adding triggers, etc that end up running in your process.
That all being said, starting in KitKat you can write to secondary storage devices through the new public APIs Context.getExternalFilesDirs() or Context.getExternalCacheDirs(). These methods are also available on ContextCompat in the support-v4 library.
Also note that if you're only interested in using the directories returned by Context, you no longer need the READ_ or WRITE_EXTERNAL_STORAGE permissions. Going forward, you'll always have read/write access to these directories with no additional permissions required.
Apps can also continue working on older devices by end-of-lifing their permission request like this:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />

Related

Android app crash with ResourceNotFoundException

I recently checked for my GP crash logs and I am getting this callstack very frequently. Could you please help me.
A few things:
The failed to add asset path sometimes shows /data/app/com.xxx.xxx.xxx-x/base.apk and sometimes /mnt/asec/com.xxx.xxx.xxx-x/base.apk.
The device android versions (where it is reported) varies from Android 4.4. to Android 7.0
In the GP console, Android version shows Android 7.0 for all crashes but the device list has devices with Android version 6.0, 5.0, etc which is unclear.
I have two apps with shared user Id, say with package names P1, P2. For P1, the failed to add asset path sometimes shows /data/app/P2 and sometimes /mnt/asec/P1. Isn't this incorrect and what could cause this?
Call stacks:
java.lang.RuntimeException: Unable to create BackupAgent android.app.backup.FullBackupAgent: android.content.res.Resources$NotFoundException: failed to add asset path data/app/com.P2.xxx/base.apk
at android.app.ActivityThread.handleCreateBackupAgent(ActivityThread.java:3452)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1796)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: android.content.res.Resources$NotFoundException: failed to add asset path /data/app/com.P2.xxx/base.apk
at android.app.ResourcesManager.createAssetManager(ResourcesManager.java:281)
at android.app.ResourcesManager.createResourcesImpl(ResourcesManager.java:359)
at android.app.ResourcesManager.getOrCreateResources(ResourcesManager.java:638)
at android.app.ResourcesManager.getResources(ResourcesManager.java:730)
at android.app.ActivityThread.getTopLevelResources(ActivityThread.java:2068)
at android.app.LoadedApk.getResources(LoadedApk.java:780)
at android.app.ContextImpl.<init>(ContextImpl.java:2244)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2184)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2175)
at android.app.ActivityThread.handleCreateBackupAgent(ActivityThread.java:3422)
Another crash:
android.content.res.Resources$NotFoundException: failed to add asset path /mnt/asec/com.P1.xxx/base.apk
at android.app.ResourcesManager.createAssetManager(ResourcesManager.java:281)
at android.app.ResourcesManager.createResourcesImpl(ResourcesManager.java:359)
at android.app.ResourcesManager.getOrCreateResources(ResourcesManager.java:638)
at android.app.ResourcesManager.getResources(ResourcesManager.java:730)
at android.app.ActivityThread.getTopLevelResources(ActivityThread.java:2052)
at android.app.LoadedApk.getResources(LoadedApk.java:787)
at android.app.ContextImpl.<init>(ContextImpl.java:2266)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2210)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2196)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5861)
at android.app.ActivityThread.-wrap3(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1710)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
Please check if the App has external drive permissions.
In my case this issue triggered when ADB installed the App to the device which is screen locked, the same test has passed when the device is awake, so even though permissions available to this App
I have this error for com.android.billingclient:billing. Problem is "there is no account in the device", for example a new device or reset to vendor settings.
So just add an account in global settings.

App was not installed error in Android sdk 19

I am using a kitkat device to debug my app,when i run the app i am getting this error.
PackageManager: mismatched uid for com.ex.app_name, try to erase old data
PackageManager: Package com.compny.app_name has mismatched uid: 10258 on disk, 10259 in settings
E/PackageManager: Unable to copy native libraries
java.io.IOException: Cannot create /mismatched_uid/settings_10259/fs_10258
at com.android.server.pm.PackageManagerService.copyNativeLibrariesForInternalApp(PackageManagerService.java:5328)
at com.android.server.pm.PackageManagerService.scanPackageLI(PackageManagerService.java:4810)
at com.android.server.pm.PackageManagerService.installNewPackageLI(PackageManagerService.java:9348)
at com.android.server.pm.PackageManagerService.installPackageLI(PackageManagerService.java:9794)
at com.android.server.pm.PackageManagerService.access$2300(PackageManagerService.java:182)
at com.android.server.pm.PackageManagerService$5.run(PackageManagerService.java:7512)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)
I have searched for this ,but i could not find a solution .Can anyone help?

Can't open file from internal storage. Code crashing in Android 7.1

Following code works perfectly for Android 5, 6. But its crashing on Android 7.1
Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(""));
intent1.setDataAndType(Uri.parse("file:///" + "sdcard/path/"+fName), "application/pdf");
startActivity(intent1);
Logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.amey.newunipunebuddy, PID: 19574
android.os.FileUriExposedException: file:///sdcard/unipunebuddy_data/timetables/Timetable.pdf exposed beyond app through Intent.getData()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
at android.net.Uri.checkFileUriExposed(Uri.java:2346)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9022)
at android.content.Intent.prepareToLeaveProcess(Intent.java:8983)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1519)
at android.app.Activity.startActivityForResult(Activity.java:4225)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
at android.app.Activity.startActivity(Activity.java:4522)
at android.app.Activity.startActivity(Activity.java:4490)
at com.amey.newunipunebuddy.Timetable$DownloadTask.onPostExecute(Timetable.java:498)
at com.amey.newunipunebuddy.Timetable$DownloadTask.onPostExecute(Timetable.java:415)
at android.os.AsyncTask.finish(AsyncTask.java:660)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
Application terminated.
From Android 6.0 onwards you need to prompt user for permission to write to external storage (Manifest.permission.WRITE_EXTERNAL_STORAGE) - https://developer.android.com/training/permissions/requesting.html (am assuming, despite title, that it is writing to external storage given use of /sdcard ?)
Google documentation on external storage
You have to use getExternalFilesDir(). Take a look at the link for other options (pictures, music,...)
Also check your permissions, I don't remember when but now Android asks you to validate permissions at app startup. Maybe it is linked to the different systems. (5, 6,...)
FileUriExposedException
You expose the uri of your file to the outside world. Which Nougat does not allow you to do any more.
Use a file provider or file content provider to serve the file.

what does this error means?

i got lots of crashes on lots of devices but on my device(lg p920) it works fine. the below report is from goggle play my app crashed and got 6 reports. by the way i use sql pre created database.and also created android_metadata table and locale en_US also inserted to that database.
this is the STACK TRACES:
android.database.sqlite.SQLiteDiskIOException: disk I/O error
at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:2074)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1014)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962)
at offline.sinhaladic.com.MainActivity.sugen(MainActivity.java:1312)
at offline.sinhaladic.com.MainActivity$7.afterTextChanged(MainActivity.java:972)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7665)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8067)
at android.text.SpannableStringBuilder.sendTextHasChanged(SpannableStringBuilder.java:900)
at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:353)
at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:266)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:443)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:420)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:29)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:663)
at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:425)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:333)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Reference : https://stackoverflow.com/a/4828540/1318946
One thought that popped up is the hard coded path to where the
database file is located. DB_PATH =
“/data/data/YOUR_PACKAGE/databases/”;
I guess it is possible that this path isn’t valid on Android 2.2.1 for
Desire HD. I exchanged this path with: Environment.getDataDirectory()
+ “/data/YOUR_PACKAGE/databases/” + DB_NAME;
I’ll try to pin point the issue when I had the chance to test on the
actual phone myself. If anyone test this solution, please let me know
if it solves it.
This is not a common error you'll see. It can happen rarely on some device with low internal memory. While your app was trying to write to DB, it ran out of internal memory and failed to write. What you can do is check the available storage and if its really dropping to zero, notify user.
SQLiteDiskIOException
java.lang.Object
↳ java.lang.Throwable
↳ java.lang.Exception
↳ java.lang.RuntimeException
↳ android.database.SQLException
↳ android.database.sqlite.SQLiteException
↳ android.database.sqlite.SQLiteDiskIOException
An exception that indicates that an IO error occured while accessing the SQLite database file.
Reference: SQLiteDiskIOException.

NullPointerException when getting Image Cache

I'm using classes from Android's BitmapFun sample code and have been running into a problem with certain devices(My app doesn't crash on all the devices I have tested, but on other peoples).
Note: The version of Android which this happens on has been: 2.3.3-2.3.7, 4.0.3-4.0.4, 4.1 and 4.2
In my Google developer console, I getting the following stack trace:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.question/com.question.ui.question}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
at android.app.ActivityThread.access$600(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4624)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.question.util.ImageCache.getDiskCacheDir(ImageCache.java:568)
at com.question.util.ImageCache$ImageCacheParams.<init>(ImageCache.java:488)
at com.question.ui.question.onCreate(question.java:58)
at android.app.Activity.performCreate(Activity.java:4479)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
... 11 more
I have looked into this and have found a question with the same problem. I have made some adjustments that are suggested from that answer and same problem still persists.
Code that is causing the problem:
public static File getDiskCacheDir(Context context, String uniqueName) {
// Check if media is mounted or storage is built-in, if so, try and use external cache dir
// otherwise use internal cache dir
final String cachePath =
Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
!isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() :
context.getCacheDir().getPath();
return new File(cachePath + File.separator + uniqueName);
}
if anyone would know what the issue is here and could explain, that would be of help!
I experienced this issue and discovered my AVD emulator was not configured correctly. Specifically, the null pointer is due to the lack of storage space defined and assigned to the Virtual Device.
To fix the problem create a new or edit your Android Virtual Device with SD Card storage. In Eclipse: Select the Andoid Virtual Device Manager ICON=> Create New=>... Near the bottom of the page is a section called SD Card: Enter a value for SD Card size.
Here is a link that describes how to configure the AVD emulator with the proper storage.
Here is a link that provides a good technical definition why the NullPointerException is generated.

Categories

Resources