Open a file passed via intent on Android 7

0

The program works in earlier versions of Android, only in Nougat.

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

Uri arqUri = intent.getData();

And I get the path to this file:

String caminho = arqUri.getPath();

The return for my tests is: /file/sdcard/Download/ss.rlc But I can not open this file on Android 7, there is an error that does not exist. In other versions of Android I can read the file normally.

If you use this path (typed manually): /storage/emulated/0/Download/ss.rlc I can usually read this file.

The file can be sent by any program such as telegram, email or others, so the problem is to get a path that can open the file in Nougat while maintaining compatibility with previous versions. The file name can be any one and the path also, by default I can not have anything fixed in the code.

Here is 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();
        }
    }
    
asked by anonymous 14.07.2017 / 18:17

1 answer

0

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

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

But I'm still looking for a more correct solution.

    
14.07.2017 / 18:41