I am uploading a file to my php webserver, but however I’m getting the permission denied. It works for mp3 and image extensions. However for .csv, i’ll get that error when i hit upload. I’m new to this and I can’t figure out if its the permission issue on android or the upload doesn’t support uploading of csv. Appreciate any guidance or help. Thanks!
The issue lies with the FileInputStream fileInputStream = new FileInputStream(sourceFile);
part where it will hit error and won’t go past there. But if its an image extention or music, it will not show the permission error and successfully uploads.
Android Studio Code
private class UploadFileAsync extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { try { String path = "/storage/emulated/0/Download"; HttpURLConnection conn = null; DataOutputStream dos = null; String lineEnd = "rn"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; File sourceFile = new File(path, "smiley.csv"); Log.d("myTag", ""+sourceFile.isFile()); if (sourceFile.isFile()) { try { String upLoadServerUri = "https://www.mywebsite.tk/upload.php?"; // open a URL connection to the Servlet Log.d("myTag", "is it here; juz before"); FileInputStream fileInputStream = new FileInputStream(sourceFile); Log.d("myTag", "is it here; juz after"); URL url = new URL(upLoadServerUri); // Open a HTTP connection to the URL conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); // Allow Inputs conn.setDoOutput(true); // Allow Outputs conn.setUseCaches(false); // Don't use a Cached Copy conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); conn.setRequestProperty("bill", "Hello.csv"); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name="bill";filename="" + "Hello.csv" + """ + lineEnd); dos.writeBytes(lineEnd); // create a buffer of maximum size bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // read file and write it into form... bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math .min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } // send multipart form data necesssary after file // data... dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) serverResponseCode = conn.getResponseCode(); String serverResponseMessage = conn .getResponseMessage(); if (serverResponseCode == 200) { // messageText.setText(msg); //Toast.makeText(ctx, "File Upload Complete.", // Toast.LENGTH_SHORT).show(); // recursiveDelete(mDirectory1); } // close the streams // fileInputStream.close(); dos.flush(); dos.close(); } catch (Exception e) { // dialog.dismiss(); e.printStackTrace(); } // dialog.dismiss(); } // End else block } catch (Exception ex) { // dialog.dismiss(); ex.printStackTrace(); } Log.d("myTag", "I iz completed"); return "Executed"; } }
My PHP Webserver Script
<?php if (is_uploaded_file($_FILES['bill']['tmp_name'])) { $uploads_dir = './'; $tmp_name = $_FILES['bill']['tmp_name']; $pic_name = $_FILES['bill']['name']; move_uploaded_file($tmp_name, $uploads_dir.$pic_name); } else{ echo "File not uploaded successfully."; } ?>
Error Logs
2021-02-12 17:11:17.047 10481-10481/com.example.assignment_test1 D/myTag: This is my message 2021-02-12 17:11:17.054 10481-10531/com.example.assignment_test1 D/myTag: true 2021-02-12 17:11:17.062 10481-10531/com.example.assignment_test1 D/myTag: is it here; juz before 2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Download/smiley.csv: open failed: EACCES (Permission denied) 2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err: at libcore.io.IoBridge.open(IoBridge.java:492) 2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:160) 2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err: at com.example.assignment_test1.MainActivity$UploadFileAsync.doInBackground(MainActivity.java:152) 2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err: at com.example.assignment_test1.MainActivity$UploadFileAsync.doInBackground(MainActivity.java:128) 2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err: at android.os.AsyncTask$3.call(AsyncTask.java:394) 2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266) 2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305) 2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err: at java.lang.Thread.run(Thread.java:923) 2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied) 2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err: at libcore.io.Linux.open(Native Method) 2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:166) 2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254) 2021-02-12 17:11:17.076 10481-10531/com.example.assignment_test1 W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:166) 2021-02-12 17:11:17.076 10481-10531/com.example.assignment_test1 W/System.err: at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7542) 2021-02-12 17:11:17.076 10481-10531/com.example.assignment_test1 W/System.err: at libcore.io.IoBridge.open(IoBridge.java:478) 2021-02-12 17:11:17.076 10481-10531/com.example.assignment_test1 W/System.err: ... 9 more 2021-02-12 17:11:17.076 10481-10531/com.example.assignment_test1 D/myTag: I iz completed
Here is the Image of my directory: Android Manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.assignment_test1"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Assignment_Test1" android:requestLegacyExternalStorage="true"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Device File Explorer Pointing to the file
Advertisement
Answer
The error you report is generated due to what is called “Scoped storage” of android 11
From the tests I have done this also occurs:
1) Create a folder (from your app) in /storage/emulated/0/Download/Test
2) Create inside a test.csv file from the app
3) Modify the content of the file or overwrite it with the user’s or server’s csv file. via usb PC, NETWORK or from Android using X-plore, Es-explorer, etc.
You just lost access to that file!
Basically you create your own directory, which did not even exist before and you find yourself not having access to the files contained in it if these are pasted manually or through another app that has access … I think it is an anomaly, I hope so we say.
The solution on Android 11 is to completely review the reading and writing of data with the new system of accessing files residing in external directories or even better is that they back off and stay (at least for directories created by the same developer) as before…hopefully in google common sense
Consult the documentation (I have not understood what I have read…I hope): Storage updates in Android 11