File Not Found with Firebase - App closes as soon as it starts - android

First of all, this is not a duplicate of Error: open failed: ENOENT (No such file or directory) I have been through this.
Second: Please read the whole question.
Third: The error was not in my code. I had to remove all filters from logcat and see this error.
This is strange, I was developing this app and using it until yesterday. Right now when I launched it on my device from Android Studio I see this error. When I remove the code that handles firebase auth and firebase UI this the error goes away.
Also, I don't know if it is related but, I had to uninstall and reinstall Android Studio and all SDKs this morning, I am facing this error after that.
I have looked at almost every resource out there, on StackOverflow and multiple gists on GitHub but none of them helped.
Any help will be appreciated.
Thank in advance.
12-26 12:17:50.061 1207-1315/? E/KernelUidCpuTimeReader: Failed to read uid_cputime
java.io.FileNotFoundException: /proc/uid_cputime/show_uid_stat: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at java.io.FileInputStream.<init>(FileInputStream.java:103)
at java.io.FileReader.<init>(FileReader.java:66)
at com.android.internal.os.KernelUidCpuTimeReader.readDelta(KernelUidCpuTimeReader.java:71)
at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8862)
at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1685)
at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:232)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
at java.io.FileInputStream.<init>(FileInputStream.java:76) 
at java.io.FileInputStream.<init>(FileInputStream.java:103) 
at java.io.FileReader.<init>(FileReader.java:66) 
at com.android.internal.os.KernelUidCpuTimeReader.readDelta(KernelUidCpuTimeReader.java:71) 
at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8862) 
at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1685) 
at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:232) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:168) 
at android.os.HandlerThread.run(HandlerThread.java:61) 
at com.android.server.ServiceThread.run(ServiceThread.java:46) 
I have required permission to read and write in manifest too
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Found the solution. For the newbies out there. The error occurred because of unstable network.
I dont know why the FileNotFound Error was thrown though. But I tested the same thing on five different devices and finally, only this makes sense. When I moved to a better internet connection the error seemed to resolve itself.
Hope this saves someone else's time.

Related

Utilizing File Api for File Access in Android 11

I'm experiencing some interesting behavior on a few Android 11 devices in regards to reading and writing to external storage. I know some changes were implemented with Android 10/11 in regards to storage but I was under the assumption than an app could access it's own app-specific directories (internal and external) without incident. Any suggestions are welcome.
Device #1: Throws the EPERM Error (Operation Not permitted) when writing to the removable external storage card with the following code:
val externalDirectories = applicationContext.getExternalFilesDirs(null)
externalFile =
File(externalDirectories[1].path + "/sampletextfile.txt")
externalFile.writeText("sample text")
Here's the stack trace:
java.io.IOException: close failed: EPERM (Operation not permitted) at libcore.io.IoBridge.closeAndSignalBlockedThreads(IoBridge.java:254) at java.io.FileOutputStream.close(FileOutputStream.java:431) at java.io.FileOutputStream.finalize(FileOutputStream.java:500) at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:291) at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:278) at java.lang.Daemons$Daemon.run(Daemons.java:139) at java.lang.Thread.run(Thread.java:923) Caused by: android.system.ErrnoException: close failed: EPERM (Operation not permitted) at libcore.io.Linux.close(Native Method) at libcore.io.ForwardingOs.close(ForwardingOs.java:88) at libcore.io.BlockGuardOs.close(BlockGuardOs.java:110) at libcore.io.ForwardingOs.close(ForwardingOs.java:88) at libcore.io.IoBridge.closeAndSignalBlockedThreads(IoBridge.java:252) at java.io.FileOutputStream.close(FileOutputStream.java:431)  at java.io.FileOutputStream.finalize(FileOutputStream.java:500)  at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:291)  at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:278)  at java.lang.Daemons$Daemon.run(Daemons.java:139)  at java.lang.Thread.run(Thread.java:923) 
Device #2: It is able to perform the above code successfully but cannot provide a list of files from a directory. The code below always yields false:
externalDirectories[1].canRead()
Also, and to add, the following always returns null even when there are files in the directory:
externalDirectories[1].listFiles()

WiFi Direct can not get established in Nearby api after Factory Reset

I am using Nearby api on two android tablets to send files between them. They can connect to each other successfully but after doing a factory reset on both devices (that is how our customers receive the device), WiFi direct cannot be established and it throws java.io.FileNotFoundException: /data/misc/wifi/networkHistory.txt (No such file or directory). Then After connecting devices to a WiFi it eventually works. Any idea what to do with this issue?
logcat:
E/WifiNetworkHistory: readNetworkHistory: No config file, revert to default, java.io.FileNotFoundException: /data/misc/wifi/networkHistory.txt (No such file or directory)
java.io.FileNotFoundException: /data/misc/wifi/networkHistory.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:146)
at java.io.FileInputStream.(FileInputStream.java:99)
at com.android.server.wifi.WifiNetworkHistory.readNetworkHistory(WifiNetworkHistory.java:331)
at com.android.server.wifi.WifiConfigManager.readNetworkHistory(WifiConfigManager.java:1920)
at com.android.server.wifi.WifiConfigManager.loadConfiguredNetworks(WifiConfigManager.java:1750)
at com.android.server.wifi.WifiConfigManager.loadAndEnableAllNetworks(WifiConfigManager.java:458)
at com.android.server.wifi.WifiStateMachine$SupplicantStartingState.processMessage(WifiStateMachine.java:4860)
at com.android.internal.util.StateMachine$SmHandler.processMsg(StateMachine.java:980)
at com.android.internal.util.StateMachine$SmHandler.handleMessage(StateMachine.java:799)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.os.HandlerThread.run(HandlerThread.java:61)
01-01 13:06:18.868 700-747/? E/IpConfigStore: Error parsing configuration: java.io.FileNotFoundException: /data/misc/wifi/ipconfig.txt (No such file or directory)

Android 6.0.1 read/write external SD card

I'm running Marshmellow 6.0.1 on a Samsung SM-T350, and I'm trying to create a file on the external sd card (/mnt/media_rw/38E5-07B6).
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I ask for permission at runtime and the user grants them. I can double check this from the system preferences. In the code I check to make sure the user granted the permissions before continuing.
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], int[] grantResults) {
check against PackageManager.PERMISSION_GRANTED
}
When I try to write to the external SD card like this:
File exportFile = new File("/mnt/media_rw/38E5-07B6/junk.txt");
exportFile.createNewFile();
I get this:
java.io.IOException: open failed: EACCES (Permission denied)
at java.io.File.createNewFile(File.java:939)
at com.myapp.util.FileUtils.copyFile(FileUtils.java:42)
at com.myapp.service.ExportService$1.onClick(ExportService.java:47)
at android.view.View.performClick(View.java:5714)
at android.widget.TextView.performClick(TextView.java:10926)
at android.view.View$PerformClick.run(View.java:22589)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at java.io.File.createNewFile(File.java:932)
at com.myapp.util.FileUtils.copyFile(FileUtils.java:42) 
at com.myapp.service.ExportService$1.onClick(ExportService.java:47) 
at android.view.View.performClick(View.java:5714) 
at android.widget.TextView.performClick(TextView.java:10926) 
at android.view.View$PerformClick.run(View.java:22589) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7325) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
When I mount the device with adb shell I grab the pid and then dump /proc/{pid}/status and I can see my user does not have the AID_SDCARD_RW (1015) permission required to write to the SD card.
Groups: 3003 9997 50143
I also found this in /system/etc/permissions/permissions.xml
<permission name="android.permission.WRITE_MEDIA_STORAGE" >
<group gid="media_rw" />
<group gid="sdcard_rw" />
</permission>
there is no android.permission.WRITE_EXTERNAL_STORAGE, but there is permission:android.permission.WRITE_MEDIA_STORAGE
So after all that, my question is this: Has anyone run into this issue where the permission is in the manifest, and they've made the changes for the new permission model, and they still can't write to the SD card? How can I (without rooting, because it shouldn't be needed) get this to work?
First, there is a WRITE_EXTERNAL_STORAGE permission. Secondly, you're assuming that external storage is /mnt/media_rw/38E5-07B6. THere is no assured hard coded path for external storage. You should request the directory instead. Third, you won't have write permission to everywhere on the disk even if you add the permission- you're limited to common directories and your app's directory. Fourth, in newer versions of Android you have to request runtime permissions as well.
So you have a lot of problems to fix.

Why do I need the WRITE_EXTERNAL_STORAGE permission with getExternalCacheDir() on Android Lollipop?

My app writes (and reads) cache files to the getExternalCacheDir() location. Before Android Lollipop (API 21) I've been using this permission with success:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
The maxSdkVersion is there because this permission shouldn't be needed after API v18:
http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE
But on Android Lollipop (5.0) I'm getting an access permission like so (with my log output to show the actual path used):
11-19 13:01:59.257 4462-4541/com.murrayc.galaxyzoo.app E/android-galaxyzoo﹕ createCacheFile(): IOException for filename=/storage/emulated/0/Android/data/com.murrayc.galaxyzoo.app/cache/52
java.io.IOException: open failed: EACCES (Permission denied)
at java.io.File.createNewFile(File.java:941)
at com.murrayc.galaxyzoo.app.provider.ItemsContentProvider.createCacheFile(ItemsContentProvider.java:528)
I see this both in the emulator and on my Nexus 4. Has something changed, or was I doing something wrong all along? I just want to access my own app's cache.
Update: I now see this only on my device (Nexus 4 running the standard Android 5.1.1, which has even had a fresh Android reflash since I first had this problem). I no longer see this in the emulator - of course the system images have been updated several times.
We've seen same behaviour on API 21 (Lollipop) on a Nexus 5:
java.io.FileNotFoundException: /storage/emulated/0/Android/data/[package name]/cache/http/journal.tmp: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:456)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
at com.android.okhttp.internal.DiskLruCache.rebuildJournal(DiskLruCache.java:346)
at com.android.okhttp.internal.DiskLruCache.open(DiskLruCache.java:239)
at com.android.okhttp.HttpResponseCache.<init>(HttpResponseCache.java:140)
at android.net.http.HttpResponseCache.install(HttpResponseCache.java:199)
...
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1011)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4518)
at android.app.ActivityThread.access$1500(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1339)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Posix.java)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:442)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
at com.android.okhttp.internal.DiskLruCache.rebuildJournal(DiskLruCache.java:346)
at com.android.okhttp.internal.DiskLruCache.open(DiskLruCache.java:239)
at com.android.okhttp.HttpResponseCache.<init>(HttpResponseCache.java:140)
at android.net.http.HttpResponseCache.install(HttpResponseCache.java:199)
...
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1011)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4518)
at android.app.ActivityThread.access$1500(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1339)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Until Google push Android 5.0 to the AOSP we won't be able to work out if its a bug or deliberate (but undocumented) change, but I've raised this bug regardless:
https://code.google.com/p/android/issues/detail?id=81357
Adding WRITE_EXTERNAL_STORAGE permission prevents the above exception being thrown, but will require end user permission to upgrade existing apps. Since our app doesn't use this permission and we don't want to add it, we're falling back to using internal cache for all except KitKat devices.
As an aside, I found this an interesting background to the changes introduced in KitKat: http://www.doubleencore.com/2014/03/android-external-storage/

Android 4.2 Lockscreen Widget image bitmaps won't load

I'm trying to enable my widget in my app on the Android 4.2 lockscreen and it works fine, however it is unable to resolve any of the image uris. The error I get is this:
java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.tvshowfavs/cache/22kifo7sdmyet7x7kphdgch69: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:416)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at java.io.FileInputStream.<init>(FileInputStream.java:105)
at android.content.ContentResolver.openInputStream(ContentResolver.java:447)
at android.widget.ImageView.resolveUri(ImageView.java:636)
at android.widget.ImageView.setImageURI(ImageView.java:381)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1146)
at android.widget.RemoteViews.performApply(RemoteViews.java:2304)
at android.widget.RemoteViews.apply(RemoteViews.java:2263)
at android.widget.RemoteViewsAdapter$RemoteViewsFrameLayout.onRemoteViewsLoaded(RemoteViewsAdapter.java:286)
at android.widget.RemoteViewsAdapter$RemoteViewsFrameLayoutRefSet.notifyOnRemoteViewsLoaded(RemoteViewsAdapter.java:335)
at android.widget.RemoteViewsAdapter$4.run(RemoteViewsAdapter.java:993)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.os.HandlerThread.run(HandlerThread.java:60)
Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:400)
Anyone have any idea why it can't open an image that is stored on the emulated storage? This image was previously downloaded and cached using the Environment.getExternalStorageDirectory(), so it should be properly written to the storage directory. This is on a Nexus 10 that I'm receiving this exception when any of the images are attempted to be loaded. Any help is appreciated.
Alright, I figured out the problem. I was using the RemoteView method setImageUri(), which it looks as though won't work since the different process can't access the file. The solution is to use setImageViewBitmap() and to decode the bitmap yourself, like this, for example: BitmapFactory.decodeFile(Uri.parse(uri).getPath())
I find it strange that the setImageUri() works fine with launchers but not on the lockscreen, however.
Tomáš was right; using a content: Uri instead of a file: Uri fixed the problem for me.
See this sample project demonstrating how to serve Files via a ContentProvider from this SO answer.

Categories

Resources