1

Following are my code in AsynTask thread:

File f = new File(Environment.getExternalStorageDirectory()+"/DCIM/IMG_20140210_130057.jpg");
 boolean exist=f.exists(); //debug result exist=false.

This code fires FileNotFoundException but the file is there.

I have already added following permission.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Lucifer
  • 29,392
  • 25
  • 90
  • 143
cc5zhenhua
  • 145
  • 3
  • 15
  • try printing out the file path you're building. – Asahi Feb 20 '14 at 12:48
  • have you seen that? http://stackoverflow.com/questions/5453708/android-how-to-use-environment-getexternalstoragedirectory – nikis Feb 20 '14 at 12:48
  • 2
    Try to add the permission to read external storage. – GrIsHu Feb 20 '14 at 12:51
  • Thank you all. After serveral trying. I found the problems is in the AsynTask . Because when i get the File code in the MainActivity UI thread, file.exists return true. But in AsynTask execution, file.exists return false. How can it be like this? – cc5zhenhua Feb 20 '14 at 14:59
  • Thanks all one more time. I finally get the problem that, in AsyncTask, when passing parameter from List to List, when convert object Back to String, it automationcally added [/mnt/sdcard...] ,this cause the FileNotFound exception. Sorry for your time. – cc5zhenhua Feb 20 '14 at 15:51

3 Answers3

0

use

File.separator

instead of '/' character that might help

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
  • hey look sometime .jpg, .jpeg, .JPG , seems different make sure with the extension of the file – Shabbir Dhangot Feb 20 '14 at 15:12
  • Thank you. I found that in MAIN activity thread, file.exists()return true but in AsycnTask's execution, file.exists()return false. So the problem is a high possibility related with the AsycnTask. – cc5zhenhua Feb 20 '14 at 15:14
0

Have you forgot to call getAbsolutePath() method ?

Try this way,

File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/DCIM/IMG_20140210_130057.jpg");
Lucifer
  • 29,392
  • 25
  • 90
  • 143
0
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Also, try to log the file path.

Log.d("Path:",Environment.getExternalStorageDirectory()+"DCIM/IMG_20140210_130057.jpg");
PsyGik
  • 3,535
  • 1
  • 27
  • 44