EACCESS permission denied on Android

When I write a file to an external SD card, I get an EACCESS permission error message. I set the permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> But when I read the file, I successfully read it, but could not write the file. The code I use to write the file to the SD card is:

 String path="mnt/extsd/Test"; try{ File myFile = new File(path, "Hello.txt"); //device.txt myFile.createNewFile(); FileOutputStream fOut = new FileOutputStream(myFile); OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); myOutWriter.append(txtData.getText()); myOutWriter.close(); fOut.close(); Toast.makeText(getBaseContext(),"Done writing SD "+myFile.getPath(),Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show(); System.out.println("Hello"+e.getMessage()); } } 

The path to the external mnt/extsd/ memory card. This is why I cannot use Environment.getExternalStorageDirectory().getAbsolutePath() , which gives me the mnt/sdcard , and this path for the internal storage path in my tablet. Please explain why this is so. How can I solve this problem.

+7
source share
4 answers

As far as I remember, Android has received partial support for several repositories since Honeycomb, and the main repository (which you get from Environment.getExternalStorageDirectory, usually part of the internal eMMC card) is still protected by WRITE_EXTERNAL_STORAGE permission, but secondary repositories (like real removable SD- map) are protected by the new resolution android.permission.WRITE_MEDIA_STORAGE , and the protection level is signatureOrSystem, see also the discussion in this article .

If so, then for a normal application it seems impossible to write anything to a real SD card without a platform signature ...

+16
source

From API level 19, Google has added an API .

  • Context.getExternalFilesDirs()
  • Context.getExternalCacheDirs()
  • Context.getObbDirs()

Applications are not allowed to write to external external storage devices, with the exception of their directories, defined in accordance with the allowed synthesized permissions. Limiting the recording in this way ensures that the system can clear files when applications are uninstalled.

Below is the approach to get the application directory on an external SD card with absolute paths.

 Context _context = this.getApplicationContext(); File fileList2[] = _context.getExternalFilesDirs(Environment.DIRECTORY_DOWNLOADS); if(fileList2.length == 1) { Log.d(TAG, "external device is not mounted."); return; } else { Log.d(TAG, "external device is mounted."); File extFile = fileList2[1]; String absPath = extFile.getAbsolutePath(); Log.d(TAG, "external device download : "+absPath); appPath = absPath.split("Download")[0]; Log.d(TAG, "external device app path: "+appPath); File file = new File(appPath, "DemoFile.png"); try { // Very simple code to copy a picture from the application's // resource into the external file. Note that this code does // no error checking, and assumes the picture is small (does not // try to copy it in chunks). Note that if external storage is // not currently mounted this will silently fail. InputStream is = getResources().openRawResource(R.drawable.ic_launcher); Log.d(TAG, "file bytes : "+is.available()); OutputStream os = new FileOutputStream(file); byte[] data = new byte[is.available()]; is.read(data); os.write(data); is.close(); os.close(); } catch (IOException e) { // Unable to create file, likely because external storage is // not currently mounted. Log.d("ExternalStorage", "Error writing " + file, e); } } 

The output from the log from above is as follows:

 context.getExternalFilesDirs() : /storage/extSdCard/Android/data/com.example.remote.services/files/Download external device is mounted. external device download : /storage/extSdCard/Android/data/com.example.remote.services/files/Download external device app path: /storage/extSdCard/Android/data/com.example.remote.services/files/ 
+7
source

I solved this problem by removing android:maxSdkVersion="18" in uses-permission in the manifest file. That is, use this:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

instead:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" /> 
0
source

Check if the user has permission to external storage or not. If not, use the cache drive to save the file.

 final boolean extStoragePermission = ContextCompat.checkSelfPermission( context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; if (extStoragePermission && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) { parentFile = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES); } else{ parentFile = new File(context.getCacheDir(), Environment.DIRECTORY_PICTURES); } 
0
source

All Articles