file.exists() returns false, can't play my audio file












0















I'm trying to play audio from storage when i click on item, for some reason file returns false for exists(), i have read and write external storage permission, and file is there and doesn't seem damaged, my file is in .mp4 format, which i will play as audio type.



this is my permissions:



<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


provider:



<provider
android:authorities="com.aliaskarurakov.android.mycallrecorderdemo"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>


xml:



<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>


this is my method:



    public void playAudio(){
String path = Environment.getExternalStorageDirectory() + "/tempAudioFile/temp.mp4";
Log.d("path", "onClick: path = "+path);
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(path);
Log.i("", "onResponse: name "+file.getName());
if (file.exists()){
Log.i("", "onResponse: I EXIST");
} else {
Log.i("", "onResponse: dont exist?");
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(getUriForFile(context,"com.aliaskarurakov.android.mycallrecorderdemo",file), "audio/*");
context.startActivity(intent);
}









share|improve this question

























  • mp4 is a video, not an audio file.

    – Fantômas
    Nov 27 '18 at 12:04











  • i know i can play it as an audio.

    – user3360453
    Nov 27 '18 at 12:06






  • 1





    I guess you should use , "video/*") instead of , "audio/*")

    – Fantômas
    Nov 27 '18 at 12:10











  • Already tried, same problem.

    – user3360453
    Nov 27 '18 at 12:18











  • Try this: intent.setDataAndType(Uri.fromFile(file), "video/*"); instead of your longer instruction.

    – Fantômas
    Nov 27 '18 at 12:27


















0















I'm trying to play audio from storage when i click on item, for some reason file returns false for exists(), i have read and write external storage permission, and file is there and doesn't seem damaged, my file is in .mp4 format, which i will play as audio type.



this is my permissions:



<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


provider:



<provider
android:authorities="com.aliaskarurakov.android.mycallrecorderdemo"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>


xml:



<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>


this is my method:



    public void playAudio(){
String path = Environment.getExternalStorageDirectory() + "/tempAudioFile/temp.mp4";
Log.d("path", "onClick: path = "+path);
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(path);
Log.i("", "onResponse: name "+file.getName());
if (file.exists()){
Log.i("", "onResponse: I EXIST");
} else {
Log.i("", "onResponse: dont exist?");
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(getUriForFile(context,"com.aliaskarurakov.android.mycallrecorderdemo",file), "audio/*");
context.startActivity(intent);
}









share|improve this question

























  • mp4 is a video, not an audio file.

    – Fantômas
    Nov 27 '18 at 12:04











  • i know i can play it as an audio.

    – user3360453
    Nov 27 '18 at 12:06






  • 1





    I guess you should use , "video/*") instead of , "audio/*")

    – Fantômas
    Nov 27 '18 at 12:10











  • Already tried, same problem.

    – user3360453
    Nov 27 '18 at 12:18











  • Try this: intent.setDataAndType(Uri.fromFile(file), "video/*"); instead of your longer instruction.

    – Fantômas
    Nov 27 '18 at 12:27
















0












0








0








I'm trying to play audio from storage when i click on item, for some reason file returns false for exists(), i have read and write external storage permission, and file is there and doesn't seem damaged, my file is in .mp4 format, which i will play as audio type.



this is my permissions:



<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


provider:



<provider
android:authorities="com.aliaskarurakov.android.mycallrecorderdemo"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>


xml:



<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>


this is my method:



    public void playAudio(){
String path = Environment.getExternalStorageDirectory() + "/tempAudioFile/temp.mp4";
Log.d("path", "onClick: path = "+path);
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(path);
Log.i("", "onResponse: name "+file.getName());
if (file.exists()){
Log.i("", "onResponse: I EXIST");
} else {
Log.i("", "onResponse: dont exist?");
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(getUriForFile(context,"com.aliaskarurakov.android.mycallrecorderdemo",file), "audio/*");
context.startActivity(intent);
}









share|improve this question
















I'm trying to play audio from storage when i click on item, for some reason file returns false for exists(), i have read and write external storage permission, and file is there and doesn't seem damaged, my file is in .mp4 format, which i will play as audio type.



this is my permissions:



<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


provider:



<provider
android:authorities="com.aliaskarurakov.android.mycallrecorderdemo"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>


xml:



<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>


this is my method:



    public void playAudio(){
String path = Environment.getExternalStorageDirectory() + "/tempAudioFile/temp.mp4";
Log.d("path", "onClick: path = "+path);
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(path);
Log.i("", "onResponse: name "+file.getName());
if (file.exists()){
Log.i("", "onResponse: I EXIST");
} else {
Log.i("", "onResponse: dont exist?");
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(getUriForFile(context,"com.aliaskarurakov.android.mycallrecorderdemo",file), "audio/*");
context.startActivity(intent);
}






android file file-exists media-type






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 12:03









Fantômas

32.7k156389




32.7k156389










asked Nov 27 '18 at 11:55









user3360453user3360453

712




712













  • mp4 is a video, not an audio file.

    – Fantômas
    Nov 27 '18 at 12:04











  • i know i can play it as an audio.

    – user3360453
    Nov 27 '18 at 12:06






  • 1





    I guess you should use , "video/*") instead of , "audio/*")

    – Fantômas
    Nov 27 '18 at 12:10











  • Already tried, same problem.

    – user3360453
    Nov 27 '18 at 12:18











  • Try this: intent.setDataAndType(Uri.fromFile(file), "video/*"); instead of your longer instruction.

    – Fantômas
    Nov 27 '18 at 12:27





















  • mp4 is a video, not an audio file.

    – Fantômas
    Nov 27 '18 at 12:04











  • i know i can play it as an audio.

    – user3360453
    Nov 27 '18 at 12:06






  • 1





    I guess you should use , "video/*") instead of , "audio/*")

    – Fantômas
    Nov 27 '18 at 12:10











  • Already tried, same problem.

    – user3360453
    Nov 27 '18 at 12:18











  • Try this: intent.setDataAndType(Uri.fromFile(file), "video/*"); instead of your longer instruction.

    – Fantômas
    Nov 27 '18 at 12:27



















mp4 is a video, not an audio file.

– Fantômas
Nov 27 '18 at 12:04





mp4 is a video, not an audio file.

– Fantômas
Nov 27 '18 at 12:04













i know i can play it as an audio.

– user3360453
Nov 27 '18 at 12:06





i know i can play it as an audio.

– user3360453
Nov 27 '18 at 12:06




1




1





I guess you should use , "video/*") instead of , "audio/*")

– Fantômas
Nov 27 '18 at 12:10





I guess you should use , "video/*") instead of , "audio/*")

– Fantômas
Nov 27 '18 at 12:10













Already tried, same problem.

– user3360453
Nov 27 '18 at 12:18





Already tried, same problem.

– user3360453
Nov 27 '18 at 12:18













Try this: intent.setDataAndType(Uri.fromFile(file), "video/*"); instead of your longer instruction.

– Fantômas
Nov 27 '18 at 12:27







Try this: intent.setDataAndType(Uri.fromFile(file), "video/*"); instead of your longer instruction.

– Fantômas
Nov 27 '18 at 12:27














1 Answer
1






active

oldest

votes


















0














You still need to ask for the permission at runtime. Call this function where needed.



private void checkReadStoragePermission(){
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,
new String{Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST_READ_EXTENAL_STORAGE);
}else{
// Do your stuff here if permission is already granted by user
}
}


And then handle if the user grants it or not.



@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
switch (requestCode){
// Here is the permission that you want
case PERMISSION_REQUEST_READ_EXTENAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
// Do your stuff here if permission is granted by user

}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499119%2ffile-exists-returns-false-cant-play-my-audio-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You still need to ask for the permission at runtime. Call this function where needed.



    private void checkReadStoragePermission(){
    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
    != PackageManager.PERMISSION_GRANTED){
    ActivityCompat.requestPermissions(this,
    new String{Manifest.permission.READ_EXTERNAL_STORAGE},
    PERMISSION_REQUEST_READ_EXTENAL_STORAGE);
    }else{
    // Do your stuff here if permission is already granted by user
    }
    }


    And then handle if the user grants it or not.



    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
    switch (requestCode){
    // Here is the permission that you want
    case PERMISSION_REQUEST_READ_EXTENAL_STORAGE:
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
    // Do your stuff here if permission is granted by user

    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }





    share|improve this answer




























      0














      You still need to ask for the permission at runtime. Call this function where needed.



      private void checkReadStoragePermission(){
      if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
      != PackageManager.PERMISSION_GRANTED){
      ActivityCompat.requestPermissions(this,
      new String{Manifest.permission.READ_EXTERNAL_STORAGE},
      PERMISSION_REQUEST_READ_EXTENAL_STORAGE);
      }else{
      // Do your stuff here if permission is already granted by user
      }
      }


      And then handle if the user grants it or not.



      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
      switch (requestCode){
      // Here is the permission that you want
      case PERMISSION_REQUEST_READ_EXTENAL_STORAGE:
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
      // Do your stuff here if permission is granted by user

      }
      super.onRequestPermissionsResult(requestCode, permissions, grantResults);
      }





      share|improve this answer


























        0












        0








        0







        You still need to ask for the permission at runtime. Call this function where needed.



        private void checkReadStoragePermission(){
        if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this,
        new String{Manifest.permission.READ_EXTERNAL_STORAGE},
        PERMISSION_REQUEST_READ_EXTENAL_STORAGE);
        }else{
        // Do your stuff here if permission is already granted by user
        }
        }


        And then handle if the user grants it or not.



        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
        switch (requestCode){
        // Here is the permission that you want
        case PERMISSION_REQUEST_READ_EXTENAL_STORAGE:
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
        // Do your stuff here if permission is granted by user

        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }





        share|improve this answer













        You still need to ask for the permission at runtime. Call this function where needed.



        private void checkReadStoragePermission(){
        if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this,
        new String{Manifest.permission.READ_EXTERNAL_STORAGE},
        PERMISSION_REQUEST_READ_EXTENAL_STORAGE);
        }else{
        // Do your stuff here if permission is already granted by user
        }
        }


        And then handle if the user grants it or not.



        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
        switch (requestCode){
        // Here is the permission that you want
        case PERMISSION_REQUEST_READ_EXTENAL_STORAGE:
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
        // Do your stuff here if permission is granted by user

        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 15 '18 at 4:59









        XelgarXelgar

        736




        736
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499119%2ffile-exists-returns-false-cant-play-my-audio-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Lallio

            Futebolista

            Jornalista