Alok
Alok

Reputation: 8988

file.exists() always fails in Android

I'm working on audio video merging and all is working fine. After merging the file it gets saved in the specified path. And this path is passed to the another activity which we get in onActivityResult and this file is played inside the videoView.

Everything is working fine, there is one thing which is causing me to stay at one place. and this is in my onAcitivityResult I'm getting the specified path, checked it in the log, but when it comes to finding the file it my file.exists() always return null.

I can see my file getting stored in the right path, but I don't know why it is not working out. I've used file.exists() in another activity and it is working fine but not on this activity.

I've done each and every possible thing to solve my problem and checked out on the internet but no results.

The things I've tried are :

File file  = new File(customVideoPath);
if(file.exists()){
  //do something
}else
//do something

I've tried this also :

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath + customVideoPath);

This is one more solution which I thought would work but did not work :

File file = new ("/mnt"+customVideoPath);

It is always to the else condition and does not perform the desired function.

Coming to the code I'm sending out the data in this code:

File passing in path form

@Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Intent intent = new Intent();
            intent.putExtra("addedAudioFile",s);    //for single file no array is passed right now
            Log.e("AUDIO_VIDEO FILE=====",s);
            setResult(RESULT_OK,intent);
            finish();
        }

This is a part of the asynch task. Now the other activity where I'm receiving the file is :

//getting the files after adding some changes to the selected video
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case CustomizeVideo:
            if (resultCode == RESULT_OK) {
                String customVideoReceived = data.getExtras().getString("addedAudioFile");
                Log.e("GETTING  IT=====", customVideoReceived);

                File file = new File(customVideoReceived);

                Log.e("FILE_CHECK",file.toString());

                if (file.exists()) {
                    //for starting the video activity
                    mVideoView.setVideoPath(customVideoReceived);
                    mController = new MediaController(EditVideoActivity.this);
                    mController.setMediaPlayer(mVideoView);
                    mVideoView.setMediaController(mController);
                    mVideoView.requestFocus();
                    mVideoView.start();
                } else {
                    Toast.makeText(getApplicationContext(), "Cannot play the file!", Toast.LENGTH_SHORT)
                            .show();
                    Toast.makeText(getApplicationContext(), "File does not merged successfully", Toast.LENGTH_SHORT)
                            .show();
                }
            }
    }
}

But all I get is the else statement implemented

Log results :

08-23 17:23:39.818 3369-3369/in.pinelane.myhovi E/AUDIO_VIDEO FILE=====: /storage/emulated/0/HovieAddTrack.mp4
08-23 17:23:39.831 3369-3369/in.pinelane.myhovi E/GETTING  IT=====: /storage/emulated/0/HovieAddTrack.mp4
08-23 17:23:39.831 3369-3369/in.pinelane.myhovi E/FILE_CHECK: /storage/emulated/0/HovieAddTrack.mp4

In the log you can clearly see that the file path is there but the in if statement is not getting implemented

And I've checked the file in the device memory, it is there ans saved I can play it, the file is working fine.

Please give a valuable suggestion so that I can move further. Thanks

In addition I've the permission in my manifest for WRITE_EXTERNAL_DATA and READ_EXTERNAL_DATA.

EDITS

I've implemented it successfully, the problem was in pathName, the pathname was not specified correct so the problem happened. Otherwise it was working fine. Thanks for the enormous support. I've learnt my lesson.

Upvotes: 3

Views: 506

Answers (2)

karthik
karthik

Reputation: 357

Try this.

File sdDir = Environment.getExternalStorageDirectory();
File file = new File(sdDir + "filename")
if(file.exists()){
Log.e("FILE_CHECK","file exists" );
}
else 
Log.e("FILE_CHECK","file dosen't exists");

Upvotes: 1

Sachin Aggarwal
Sachin Aggarwal

Reputation: 1135

Try giving just the name of audio file as argument i.e.

File myFile = new File(audioFilename);

I was having the same problem once and it worked. Sounds weird but it worked.

Upvotes: 1

Related Questions