0

I am trying to browse a file when a button is pressed and when the file is selected I print a toast of absolute path. Following is my code

 browse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                radioGroup.clearCheck();
                //Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("audio/*");
                startActivityForResult(intent,7);
            }
        });

And below is onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
super.onActivityResult(requestCode,resultCode,data);
        switch (requestCode) {
            case 7:
                if (resultCode == RESULT_OK) {
                    Uri uri = null ;
                    if (data !=null){
                        uri = data.getData(); //= data.getData();

                    try {
                        String mimeType = getContentResolver().getType(uri);
                        String path = cls2.getRealPath(this, uri);

                        Toast.makeText(MainActivity.this, (CharSequence) path, Toast.LENGTH_LONG).show();
             break;
                }catch(Exception e)
                {
                    Log.e("Exception found ",e.getMessage());
                }
            }
    }
    }
}

where cls2.getRealPath()

i.e.

RealPathUtil cls2 = new RealPathUtil();

getRealPath method and RealPathUtil class I got from following link

https://gist.github.com/tatocaster/32aad15f6e0c50311626

Above code is perfectly working in Android Lollipop, but when I run above code in Android Nougat , I don't get any path . What might the reason, does Nougat has got different API to access absolute path. And I have written the required permissions in the manifest file.

ankuranurag2
  • 2,300
  • 15
  • 30
  • "Above code is perfectly working in Android lollipop" -- only for the scenarios that you tested. – CommonsWare Feb 25 '18 at 15:08
  • can you tell me which question so that i can explain, with due respect – stunningbeauty Feb 25 '18 at 15:10
  • I linked to [the duplicate question](https://stackoverflow.com/questions/35870825/getting-the-absolute-file-path-from-content-uri-for-searched-images). – CommonsWare Feb 25 '18 at 15:12
  • she is sharing images from other applications to her application – stunningbeauty Feb 25 '18 at 15:26
  • That does not change the nature of the problem. Both of you think that a `Uri` points to a file. It might, if the scheme of the `Uri` is `file`. If the scheme is anything else (`http`, `https`, `content`, `android.resource`, etc.), then the `Uri` does not point to a file. The answers on that question point out what to do to handle more types of `Uri` values. Here is [another duplicate question](https://stackoverflow.com/a/48510756/115145), also dealing with trying to treat a `Uri` as a file. – CommonsWare Feb 25 '18 at 15:31
  • So does nougot have same way to access files as lollipop ? – stunningbeauty Feb 25 '18 at 15:50
  • Your code is not accessing files. It is accessing content. That content *may* come from files. But, as [I point out here](https://stackoverflow.com/a/35870955/115145), it may come from something else. Your code does not work on any version of Android very well. It handles content from a few apps (at least until those apps get updated and the rules change). Your code fails for everything else. So, get rid of that code, and either work with just streams, or make your own copy of the content to some file that you control. – CommonsWare Feb 25 '18 at 15:58

0 Answers0