FileReader path error - File Not Found - android

I have included my root-CA.crt file in app/src/main/assets.
I have typed this and ran on the emulator.
PEMParser reader = new PEMParser(new FileReader("file:///android_asset/root-CA.crt"));
X509CertificateHolder caCertHolder = (X509CertificateHolder) reader.readObject();
reader.close();
And got this error:
10-18 20:47:34.601 10617-10617/? W/System.err: java.io.FileNotFoundException: /file:/android_asset/root-CA.crt: open failed: ENOENT (No such file or directory)
10-18 20:47:34.601 10617-10617/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:416)
10-18 20:47:34.601 10617-10617/? W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:78)
10-18 20:47:34.601 10617-10617/? W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:105)
10-18 20:47:34.611 10617-10617/? W/System.err: at java.io.FileReader.<init>(FileReader.java:66)
Why I am getting this error ?? I have already mentioned the correct path. But why?

file:///android_asset is only meaningful with WebView. It cannot be used elsewhere.
Use AssetManager and its open() method to get an InputStream on the content represented by your asset. Wrap that in an InputStreamReader as needed.

Related

Xamarain Forms file read permissions issue on Android. ENOENT (No such file or directory). Only happens for some users

I've got a Xamarin.Forms app on the Google Play store for changing the pitch and speed of music and video. Its got a file read permissions problem on Android, but only for some users (stack trace shown below). I've set the external storage read permission:
uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
in the manifest.
For the majority of users, all is working correctly when selecting an audio/video file. But I'm getting notified via Rollbar https://rollbar.com/ of the file permissions issues for some users in android versions 8-11.
I'm using the https://www.nuget.org/packages/Xamarin.Plugin.FilePicker/ plugin to browse for music/video files in my app.
Rollbar error notification from my app:
Java.IO.FileNotFoundException: /storage/emulated/0/Download/Kev Hlub Tu Tag Nag Hmo (Lyric).mp4: open failed: ENOENT (No such file or directory) ---> Android.Systems.ErrnoException: open failed: ENOENT (No such file or directory)
Stack trace:
Java.IO.FileNotFoundException: /storage/emulated/0/Download/Kev Hlub Tu Tag Nag Hmo (Lyric).mp4: open failed: ENOENT (No such file or directory) ---> Android.Systems.ErrnoException: open failed: ENOENT (No such file or directory)
--- End of inner exception stack trace ---
at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in <24e422c426e0468ca1fd74b59870ff08>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <24e422c426e0468ca1fd74b59870ff08>:0
at Android.Media.MediaPlayer.SetDataSource (Android.Content.Context context, Android.Net.Uri uri) [0x00053] in <4586a87d34754df7a521e11e2fd22040>:0
at FormsVideoLibrary.Droid.VideoPlayerRenderer.SetSource () [0x0003e] in <01a54421e2d344a3a06c7dbb0d52b8a1>:0
--- End of managed Java.IO.FileNotFoundException stack trace ---
java.io.FileNotFoundException: /storage/emulated/0/Download/Kev Hlub Tu Tag Nag Hmo (Lyric).mp4: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:492)
at java.io.FileInputStream.<init>(FileInputStream.java:160)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1259)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1230)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1147)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1072)
at crc646e83aba39dfdd46d.VideoPlayerRenderer.n_surfaceCreated(Native Method)
at crc646e83aba39dfdd46d.VideoPlayerRenderer.surfaceCreated(VideoPlayerRenderer.java:70)
at android.view.SurfaceView.updateSurface(SurfaceView.java:1284)
at android.view.SurfaceView$1.onPreDraw(SurfaceView.java:225)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1124)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3854)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2618)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9971)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1010)
at android.view.Choreographer.doCallbacks(Choreographer.java:809)
at android.view.Choreographer.doFrame(Choreographer.java:744)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:995)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8373)
at libcore.io.IoBridge.open(IoBridge.java:478)
... 24 more
My App on Google Play:
https://play.google.com/store/apps/details?id=com.darceysoft.mavsc&hl=en&gl=US
Thanks in advance for any help on this.
Cheers,
Wayne

Want to crop video in 1:1 aspectration using ffmpeg but it gives error Working Directory: null Environment: null

I have used below command for crop video from originally captured by default camera. But when i was execute command it gives below error.
final String[] cmd={"-i", "/storage/emulated/0/DCIM/ZyfVideo/VID_20170914_153804.mp4","-s", "480x480" ,"-vf" ,"crop=480:480","setdar=1:1","setsar=1:1 ","/storage/emulated/0/Movies/SpoofApp/VDO_20170914_153811.mp4"};
Error Logcat :
09-14 15:38:11.874 3872-4039/com.spoof.app E/FFmpeg: Exception while trying to run: [Ljava.lang.String;#427ce088
java.io.IOException: Error running exec(). Command: [/data/data/com.spoof.app/files/ffmpeg, -i, /storage/emulated/0/DCIM/ZyfVideo/VID_20170914_153804.mp4, -s, 480x480, -vf, crop=480:480, setdar=1:1, setsar=1:1 , /storage/emulated/0/Movies/SpoofApp/VDO_20170914_153811.mp4] Working Directory: null Environment: null
at java.lang.ProcessManager.exec(ProcessManager.java:211)
at java.lang.Runtime.exec(Runtime.java:173)
at java.lang.Runtime.exec(Runtime.java:128)
at com.github.hiteshsondhi88.libffmpeg.ShellCommand.run(ShellCommand.java:10)
at com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteAsyncTask.doInBackground(FFmpegExecuteAsyncTask.java:38)
at com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteAsyncTask.doInBackground(FFmpegExecuteAsyncTask.java:10)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.io.IOException: No such file or directory
at java.lang.ProcessManager.exec(Native Method)
at java.lang.ProcessManager.exec(ProcessManager.java:209)
at java.lang.Runtime.exec(Runtime.java:173) 
at java.lang.Runtime.exec(Runtime.java:128) 
at com.github.hiteshsondhi88.libffmpeg.ShellCommand.run(ShellCommand.java:10) 
at com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteAsyncTask.doInBackground(FFmpegExecuteAsyncTask.java:38) 
at com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteAsyncTask.doInBackground(FFmpegExecuteAsyncTask.java:10) 
at android.os.AsyncTask$2.call(AsyncTask.java:288) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
at java.lang.Thread.run(Thread.java:841) 
I have no such idea about ffmpeg but i can tell you about jCodec lib which is easy to use.
visit jcodec.org.
if you want to crop the video you will have to do below step in android or java.
1. get the frame from utility method from jcodec.
2. crop using Bitmap helper class.
3. pack in the mp4 format using jcodec utilty function.
if you are not able to do this kindly write a mail to me sunilgupta3443#gmail.com
I will help to make the crop video application for you in android.
Thank you

Android Emulator Black screen -

Application worked on this emulator, I have tried to implement Google Play Game services and it appears that this service can't connect. I have tried on multiple emulators. Now when I run the app all I get is the black screen.
Here is the Android Monitor:
09-18 13:11:28.507 1141-1141/? E/Drm: Failed to open plugin directory /vendor/lib/mediadrm
09-18 13:11:29.897 1963-8847/com.google.android.gms E/ChromeSync: [Sync,SyncAdapter] Failed to sync.
hlr: Error when calling the server (message: Application credential header not valid. Please fix the client to pass a valid application credential header.).
at hlt.b(:com.google.android.gms:11)
at hmd.c(:com.google.android.gms:1)
at hmh.b(:com.google.android.gms:61)
at hmf.a(:com.google.android.gms:12)
at jlo.onPerformSync(:com.google.android.gms:2)
at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:259)
Caused by: aury: INVALID_ARGUMENT: Application credential header not valid. Please fix the client to pass a valid application credential header.
at auru.c(:com.google.android.gms:2)
at jgq.a(:com.google.android.gms:62)
at hlt.b(:com.google.android.gms:9)
at hmd.c(:com.google.android.gms:1) 
at hmh.b(:com.google.android.gms:61) 
at hmf.a(:com.google.android.gms:12) 
at jlo.onPerformSync(:com.google.android.gms:2) 
at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:259) 
09-18 13:11:41.977 8863-8863/? E/memtrack: Couldn't load memtrack module (No such file or directory)
09-18 13:11:41.977 8863-8863/? E/android.os.Debug: failed to load memtrack module: -2
09-18 13:11:42.887 1963-1972/com.google.android.gms E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:179)
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:168)
at android.os.ParcelFileDescriptor.createPipe(ParcelFileDescriptor.java:362)
at com.google.android.gms.ads.internal.request.n.a(:com.google.android.gms:1)
at com.google.android.gms.ads.internal.request.n.writeToParcel(:com.google.android.gms:5)
at jdq.a(:com.google.android.gms:16)
at com.google.android.gms.ads.internal.request.d.writeToParcel(:com.google.android.gms:28)
at brr.a(:com.google.android.gms:6)
at com.google.android.gms.ads.internal.request.k.a(:com.google.android.gms:0)
at com.google.android.gms.ads.internal.request.service.i.run(:com.google.android.gms:7)
at com.google.android.gms.ads.internal.util.v.call(:com.google.android.gms:1)
at com.google.android.gms.ads.internal.util.w.run(:com.google.android.gms:0)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
09-18 13:11:42.957 1816-2482/com.google.process.gapps E/NetworkScheduler.SR: Unrecognised action provided: android.intent.action.PACKAGE_REMOVED
09-18 13:11:43.397 8903-8903/? E/memtrack: Couldn't load memtrack module (No such file or directory)
Also I tried to install signed apk to Emulator with adb install but the same problem appears.

Using android internal storage for images

I am trying to save the image file into my application's internal storage.
To do this, I wrote the following method which takes the id and bitmap and creates the file of an image, where directory is the class static field of type File.
// Returns thumbnail
private static Bitmap saveBitmapWithThumbnail(long id, Bitmap bitmap) {
Bitmap thumbnail;
if(directory == null) {
ContextWrapper cw = new ContextWrapper(Engine.getContext());
directory = cw.getDir(DIRECTORY_NAME, Context.MODE_PRIVATE);
if(!directory.exists()) directory.mkdir();
}
saveBitmap(getImageFileName(id), bitmap);
saveBitmap(getThumbnailFileName(id), thumbnail=makeThumbnail(bitmap));
return thumbnail;
}
What this method does is to get an id and bitmap and save it's original file and created thumbnail into the internal storage.
The names are generated as [id+".png"] for the original file and [id+"_tn.png"] for the thumbnail file.
saveBitmap() mehtod is the one which is writing the bitmap to the internal storage.
private static void saveBitmap(String fileName, Bitmap bitmap) {
File imageFile = new File(directory, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch(Exception e) {}
}
It takes the file name (11.png, 11_tn.png, ...) and the bitmap (whether it's original or thumbnail) and makes a file and compress into it's outputstream.
The weird thing is that when I run my application for the first time saving the images, it works great.
It can read and write the images that are stored int the internal storage.
However, after I close my application and run it again, it throws FileNotFoundException with following logs.
W/System.err: java.io.FileNotFoundException: /1.png: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:458)
W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:78)
W/System.err: at com.google.dotplace.dotplace.data.Image.parseCursor(Image.java:118)
W/System.err: at com.google.dotplace.dotplace.data.Image.checkDebug(Image.java:74)
W/System.err: at com.google.dotplace.dotplace.LoadActivity.debugDatabase(LoadActivity.java:77)
W/System.err: at com.google.dotplace.dotplace.LoadActivity.onCreate(LoadActivity.java:25)
W/System.err: at android.app.Activity.performCreate(Activity.java:5275)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2167)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2253)
W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:142)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:136)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5120)
W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
W/System.err: at dalvik.system.NativeStart.main(Native Method)
W/System.err: Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:442)
W/System.err: ... 19 more
Only conclusion I can make is that internal storage changes or it's files are delete after my application is closed, but I don't know why.
What I am guessing now is that I have problem with my testing.
I'm testing this as follows.
Compile and run the project with file insertions and check.
Compile and run the project without file insertions and check if 1's data is remaining.
I am doing this because I did't make any ui of writing or reading files in my app.
My only guess is when I compile the project again, it somehow changes something unique in the context of my application which leads to changed internal storage which I think it's quite nonsense.
Where am I wrong??
There are several things which you may be doing wrong. Whenever you want to read/write internal/external storage, you have to make sure you do the following things;
Put READ/WRITE Storage permissions in your Manifest.
If your Android Version is Marshmallow or greater, ask run-time permissions. You can get information about this here
Make sure the path is correct. Most of the time hard-coded paths make difference. Refer here for more detail.
Make sure you call mkdirs() if directory doesn't exist.
Make sure the filename is correct. You said earlier that it's 11.png and 11_tn.png but later you accessed 1.png. May be this is causing a problem.
you have to give runtime permission for marshmallow and above:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />`

How do I fix an ANR handler throwing an exception?

I am debugging using an actual phone, and sometimes I am inspecting the contents of an Intent (which naturally arrives on the main thread) in the debugger. However, since I am blocking the main thread, the system throws an ANR. Afterwards, the app crashes and I see this in LogCat:
Here is the exception:
07-25 12:25:32.058 673-15203/system_process E/ActivityManager﹕ Error reading /data/anr/traces.txt
java.io.FileNotFoundException: /data/anr/traces.txt: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:409)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at android.os.FileUtils.readTextFile(FileUtils.java:159)
at com.android.server.am.ActivityManagerService$15.run(ActivityManagerService.java:8926)
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:393)
            at java.io.FileInputStream.<init>(FileInputStream.java:78)
            at android.os.FileUtils.readTextFile(FileUtils.java:159)
            at com.android.server.am.ActivityManagerService$15.run(ActivityManagerService.java:8926)
Terminal shows that the file is actually not there. If I try and create it myself, it just gets deleted and the exception is still thrown. There is actually a different file: traces_com.foo.bar.txt. Maybe it is supposed to be looking for that, but isnt for some reason?

Categories

Resources