Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read file in android - file permission denied

I want to read file from external storage. I uploaded the file in Eclipse DDMS to storage/sdcard (image below). But whenever I tried to read, I got an error of permission denied. Is there any problem in my file permission? Or I need to add anything in manifest file (I am not writing anything at the moment)?

Any help will be appreciated.

Folder Tree

Code:

public void extimport(View v){
   EditText xedittxt = (EditText) findViewById(R.id.frmexttxt);
   String xedit = xedittxt.getText().toString();
   xedit = xedit.trim();
   File file;
   file = new File(xedit);
   StringBuilder text = new StringBuilder();
   Log.d("fcheck",""+xedit);
   try {
      BufferedReader br = new BufferedReader(new FileReader(file)); //THIS LINE THROWS ERROR
      Log.d("fcheck","f3"); //This line never got printed
      String line;
      while ((line = br.readLine()) != null) {
         text.append(line);
         text.append('\n');
      }
      br.close();
      resultView = (TextView) findViewById(R.id.header);
      resultView.setText(text);
   }
   catch (IOException e) {
      Log.d("File open error",""+e);
      Toast.makeText(getApplicationContext(), "Error opening the file.", Toast.LENGTH_SHORT).show();
   }
}

LogCat:

11-19 00:21:54.252: D/fcheck(5885): /storage/sdcard/mylibman.csv
11-19 00:21:54.272: D/File open error(5885): java.io.FileNotFoundException: /storage/sdcard/mylibman.csv: open failed: EACCES (Permission denied)
like image 712
abdfahim Avatar asked Nov 19 '13 05:11

abdfahim


People also ask

How do you get read and write permissions in Android?

To read and write data to external storage, the app required WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system permission. These permissions are added to the AndroidManifest. xml file. Add these permissions just after the package name.

How do I give permission to access all files in Android?

On the Settings > Privacy > Permission manager > Files and media page, each app that has the permission is listed under Allowed for all files. If your app targets Android 11, keep in mind that this access to "all files" is read-only.

What is permission protection level in Android?

The three permission protection levels in Android are as follows: Normal Permissions. Signature Permissions. Dangerous Permissions.


2 Answers

Make sure you have added the permission to read external storage in your manifest file.

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
like image 192
GrIsHu Avatar answered Sep 20 '22 11:09

GrIsHu


I was experiencing the same problem and it can be easily removed by following either of these steps: 1. Install your app by using -g if installing on Android N and O versions. 2. Grant permission manually Settings->apps->"app_name"->Permissions->Enable Switch

For Both steps 1 and 2, define uses-permission

android:name="android.permission.WRITE_EXTERNAL_STORAGE" and uses-permission 
android:name="android.permission.READ_EXTERNAL_STORAGE" in AndroidManifest.xml

Like this :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="...">


    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <!-- all permissions here -->
    <application
        ...
like image 33
Vsw10 Avatar answered Sep 20 '22 11:09

Vsw10