Open a file passed via Intent on Android 7

Asked

Viewed 208 times

0

The program works on previous versions of Android, only on Nougat that does not.

I get the file from some program, for example the file manager:

Uri arqUri = intent.getData();

And get the path to that file:

String caminho = arqUri.getPath();

The comeback in the case of my tests is : /file/sdcard/Download/ss.rlc But I cannot open this file on Android 7, error occurs that does not exist. On other versions of Android I can read the file normally.

If I use this path (manually typed):/Storage/Emulated/0/Download/ss.rlc I can normally read this file.

The file can be sent by any program like Telegram, email or others, so the problem is to get a path that is possible to open the file in Nougat maintaining compatibility with previous versions. The file name could be anyone and the path too, so I can’t have anything fixed in the code.

Here’s the part of the code where I try to read the file:

        Uri arqUri = intent.getData();
    if (arqUri != null) {
        try {
            FileInputStream fis;
            String caminho = arqUri.getPath();
            Log.d("Artur", "caminho : "+caminho);
            //File fx = new File("/storage/emulated/0/Download/ss.rlc"); //FUNCIONA
            File fx = new File(caminho);
            fis = new FileInputStream(fx);

            fis.close();

        } catch (FileNotFoundException e) {
            Log.d("Artur", "Erro: "+e.toString());
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • I think the problem has to do with the changes in permissions on file sharing, as explained in this reply.

  • Interesting and thank you. But I believe that this would result in another problem. In the description I have to list the directories I need access to. And I do not know what they are. I will only know when to use because I do not know which application is being used to send the file. It can be Telegram, Wats or other chat. It can be any email program. Or any form of file sharing. As I posted below I have the right of access, only the beginning of the path that must be changed.

1 answer

0

I got a not pretty outline but it worked for Telegram and the file manager:

caminho = caminho.replace("/media","/storage/emulated/0");
caminho = caminho.replace("/file/sdcard","/storage/emulated/0");

But I still seek a more correct solution.

Browser other questions tagged

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