Filenotfoundexception - Android Studio

Asked

Viewed 81 times

0

I have the following code:

String f = getStringFromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/PagamentoLio.json");



String getStringFromFile (String filePath)  {
    File fl = new File(filePath);
    FileInputStream fin = null;
    String error = "";
    try {
        fin = new FileInputStream(fl);

        String ret = convertStreamToString(fin);
        //Make sure you close all streams.
        fin.close();

        return ret;
    } catch (FileNotFoundException e) {
        error = e.getMessage();
        e.printStackTrace();
    } catch (IOException e) {
        error = e.getMessage();
    } catch (Exception e) {
        error = e.getMessage();
    }

    showMessageError(error);

    return "";
}

But when he gets on the line fin = new FileInputStream(fl); is pitching FileNotFoundException, the problem is that the file exists and is on the right track. I tried with other files and the same thing happens. What might be causing this?

inserir a descrição da imagem aqui

  • 1

    Did you declare/request permission to read the external storage? Because the comment itself indicates that permission has been denied

  • I think that’s exactly what’s missing... I’ll try to resolve. Thank you!

1 answer

2


To read or write to external storage, you must request permission.

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

For Level 23 API (Android 6.0), you must request permission at runtime.

The following code checks if the application has the permission to read the user’s external storage and requests permission if necessary.

if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED) {

    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // Requisita a permissão

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                1000);
    }
}

Deals with request of requested permission

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1000: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

https://developer.android.com/training/permissions/requesting?hl=pt-BR

  • Remember that requestpermission is an async method, and it may also be important to treat when the user clicks on "dont Ask Again"

  • Well remembered @Murillocomino

Browser other questions tagged

You are not signed in. Login or sign up in order to post.