Nicolas400
Nicolas400

Reputation: 85

Can't read file from SDCARD Android Application

I'm trying to read a json file from the SDCard in the Phone.(SAMSUNG SM-G532M).

But I can't.

I want to put the file in "Downloads" folder, and make the app to look in that folder in particular, for a particular filename.

But i get a FileNotFoundException.

When I debug the application, the path is different from what i spected. I get "/storage/emulated/0", but I want to read the Download Folder in the SDCARD.

When i use this sentece:

ruta_sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

and I debug the value, it's ponting to :

 /storage/emulated/0/Download

When I try to navigate with Device File Explorer, i get "Opendir Failed: Permission Denied"

What i'm doing wrong ?

I added this line to manifest.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">

</uses-permission>

notes: Developing un Android Studio 3.2 Phone : Samsung : SM-G532M ( not emulated )

Thanks in advance! Best Regards

Upvotes: 0

Views: 1972

Answers (1)

Ramesh Yankati
Ramesh Yankati

Reputation: 1217

You need to request the runtime.

public static final int READ_EXTERNAL_STORAGE = 112;


protected void readSDcardDownloadedFiles() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE);
    } else {
       //Permission is granted
       //Call the method to read file. 
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        //Read the files
    } else {
        // permission denied, boo! Disable the
        // functionality that depends on this permission.
    }
}

Upvotes: 3

Related Questions