I am trying to use an Android app to upload a file to a node js server on my laptop. While it works fine with html, when trying to do the same in Andorid I get the following error.
The server is a simple Node server which seems to be working fine when I use other methods of posting files.
Error in upload with ID: 10ff84ba-0768-4423-a955-7a1a4f68d1aa. /init.rc: open failed: EACCES (Permission denied)
java.io.FileNotFoundException: /init.rc: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:456)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at com.alexbbb.uploadservice.BinaryUploadFile.getStream(BinaryUploadFile.java:34)
at com.alexbbb.uploadservice.BinaryUploadTask.writeBody(BinaryUploadTask.java:29)
at com.alexbbb.uploadservice.HttpUploadTask.upload(HttpUploadTask.java:131)
at com.alexbbb.uploadservice.HttpUploadTask.run(HttpUploadTask.java:65)
at com.alexbbb.uploadservice.UploadService.onHandleIntent(UploadService.java:127)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
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 libcore.io.IoBridge.open(IoBridge.java:442)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at com.alexbbb.uploadservice.BinaryUploadFile.getStream(BinaryUploadFile.java:34)
at com.alexbbb.uploadservice.BinaryUploadTask.writeBody(BinaryUploadTask.java:29)
at com.alexbbb.uploadservice.HttpUploadTask.upload(HttpUploadTask.java:131)
at com.alexbbb.uploadservice.HttpUploadTask.run(HttpUploadTask.java:65)
at com.alexbbb.uploadservice.UploadService.onHandleIntent(UploadService.java:127)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
01-01 08:17:17.382 20517-25217/jorc.com.fileupload2 W/com.alexbbb.uploadservice.BinaryUploadTask: Error in uploadId 5d59b0bb-5f5f-4926-b17d-87a8e408af4f on attempt 1. Waiting 1s before next attempt java.io.FileNotFoundException: /init.rc: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:456)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at com.alexbbb.uploadservice.BinaryUploadFile.getStream(BinaryUploadFile.java:34)
at com.alexbbb.uploadservice.BinaryUploadTask.writeBody(BinaryUploadTask.java:29)
at com.alexbbb.uploadservice.HttpUploadTask.upload(HttpUploadTask.java:131)
at com.alexbbb.uploadservice.HttpUploadTask.run(HttpUploadTask.java:65)
at com.alexbbb.uploadservice.UploadService.onHandleIntent(UploadService.java:127)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
Trying later I get the following confirmation from logcat but still no file is uploaded to my server:
01-01 08:57:43.032 1837-1837/jorc.com.fileupload2 I/UploadServiceDemo: Upload with ID 2b47d6b5-7bf6-4cf0-aeec-462b20c40ffd is completed: 200, <!DOCTYPE html>
<html>
<head>
<title>File Uploader</title>
<link href="app.css" rel="stylesheet">
</head>
It seems you are using this repo from githib:
https://github.com/alexbbb/android-upload-service
in this case go to MainActivity.java file and add
.addHeader("X-Parse-Application-Id", "your_key")
.addHeader("X-Parse-REST-API-Key", "your_key2")
under
.addHeader("file-name", new File(fileToUploadPath).getName())
That's all you need to see your file being uploaded to parse.com.
To make it easier on device add this:
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage(serverResponseMessage);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
Under this which shows you the uploaded file URL.
public void onCompleted(String uploadId, int serverResponseCode, String serverResponseMessage) {
progressBar.setProgress(0);
It is clearly written in your logcat Permission denied, you need to give READ permission in your manifest.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Related
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
I am trying to move file from internal phone memory to sd card.
val oldFile = File(oldPath) //oldPath is string
val newFile = File(newPath) //newPath is string
if(newFile.exists()) {
newFile.delete()
}
newFile.parentFile.mkdirs()
newFile.createNewFile() //crashes here
// copy file code
Paths are absolute.
Old file path is:
/storage/emulated/0/Android/data/com.package/files/2ec37ce3-ca72-4a35-a6e6-2d7f8e864c6c
New file path is:
/storage/48EE-C144/chosenDirectory/2ec37ce3-ca72-4a35-a6e6-2d7f8e864c6c
where 2ec37ce3-ca72-4a35-a6e6-2d7f8e864c6c is file.
Paths are proper and the new file path is chosen manually by user with external directory picker library which works fine.
Of course app asks user for permissions and does not shall pass user to app until permissions are not granted.
int perm = this.checkSelfPermission(permission.WRITE_EXTERNAL_STORAGE);
int perm2 = this.checkSelfPermission(permission.READ_EXTERNAL_STORAGE);
if (perm != PackageManager.PERMISSION_GRANTED || perm2 != PackageManager.PERMISSION_GRANTED) {
/*ask for permissions and show explain dialog*/
}
Parmissions in manifest are added too.
Application crashes while calling newFile.createNewFile() with exception log:
05-07 13:02:14.995 14004-14004/com.packagename E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.packagename, PID: 14004
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
Caused by: java.io.IOException: Permission denied
at java.io.UnixFileSystem.createFileExclusively0(Native Method)
at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
at java.io.File.createNewFile(File.java:948)
at com.packagename.backend.localApi.changePath.PathChanger.changePath(PathChanger.kt:29)
at com.packagename.backend.localApi.changePath.PathChangeService$changePath$1.onSelect(PathChangeService.kt:49)
at com.codekidlabs.storagechooser.fragments.ChooserDialogFragment$1.onItemClick(ChooserDialogFragment.java:169)
at android.widget.AdapterView.performItemClick(AdapterView.java:343)
at android.widget.AbsListView.performItemClick(AbsListView.java:1665)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:4075)
at android.widget.AbsListView$10.run(AbsListView.java:6552)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6816)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
Need to add permission in AndroidManifest.xml file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Solution for me was stop using java classes and migrate to Storage Access Framework
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" />`
I'm getting an exception that file open permission is denied when I'm trying to open a FileInputStream.
File myFile = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/test/test.txt");
try {
FileInputStream inStream = new FileInputStream(myFile); // crash
Here's the stacktrace:
System.err﹕ java.io.FileNotFoundException: /storage/emulated/0/test/test.txt: open failed: EACCES (Permission denied)
System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:456)
System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:76)
...
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 libcore.io.IoBridge.open(IoBridge.java:442)
And yes, I do have the permissions in my manifest:
<uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE" />
Android is case sensitive. Replace ANDROID.PERMISSION with android.permission:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Also, you should only need one of those permissions. If you are planning on writing to external storage, you should not also need READ_EXTERNAL_STORAGE.
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?