Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: open failed: ENOENT (No such file or directory)

Tags:

android

I was trying to create a file to save pictures from the camera, it turns out that I can't create the file. But I really can't find the mistake. Can you have a look at it and give me some advice?

    private File createImageFile(){             File imageFile=null;             String stamp=new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());             File dir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);             String imageFileName="JPEG_"+stamp+"_";             try {                 imageFile=File.createTempFile(imageFileName,".jpg",dir);             } catch (IOException e) {                 Log.d("YJW",e.getMessage());             }             return  imageFile;         } 

And I have added the permission.

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

The method always gives such mistakes:

open failed: ENOENT (No such file or directory)

like image 669
Jiawei Yang Avatar asked Mar 18 '16 15:03

Jiawei Yang


People also ask

What is Enoent error in Android?

The ENOENT (No such file or directory) error may occur in application, If the application do not have storage permission. We suggest you to ensure whether the application has storage permission to read and write file in storage location. We have modified sample to provide storage permission at run time.

What is Enoent error?

It's an abbreviation of Error NO ENTry (or Error NO ENTity), and can actually be used for more than files/directories. It's abbreviated because C compilers at the dawn of time didn't support more than 8 characters in symbols. Follow this answer to receive notifications.

Why is there no such file or directory?

log No such file or directory” the problem is most likely on the client side. In most cases, this simply indicates that the file or folder specified was a top-level item selected in the backup schedule and it did not exist at the time the backup ran.


1 Answers

The Pictures directory might not exist yet. It's not guaranteed to be there.

In the API documentation for getExternalStoragePublicDirectory(), the code ensures the directory exists using mkdirs:

File path = Environment.getExternalStoragePublicDirectory(         Environment.DIRECTORY_PICTURES); File file = new File(path, "DemoPicture.jpg");  try {     // Make sure the Pictures directory exists.     path.mkdirs();  

...so it may be as simple as adding that path.mkdirs() to your existing code before you createTempFile.

like image 99
Matt Gibson Avatar answered Oct 01 '22 13:10

Matt Gibson