0

File not found exception:java.io.FileNotFoundException: /storage/emulated/0/FolderName/subFolderName/my_image.jpg: open failed: ENOENT (No such file or directory)

I am getting above exception in some phones only

I am getting File not found exception while saving image captured from camera. Here is my code

I am capturing image :

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                        startActivityForResult(takePictureIntent, 0);
                    }

In onActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0)
    {
        if(data!=null)
        {
            Bundle extras = data.getExtras();
            if (extras != null)
            if (img1) {
                imgAdd1.setVisibility(View.GONE);
                Bitmap thumbnail_1 = (Bitmap) extras.get("data");
                Bitmap resized = scaleBitmap(thumbnail_1) ;
                selectedImage1 = data.getData() ;
                //Bitmap thumbnail_1 = (Bitmap) data.getExtras().get("data");
                image1.setVisibility(View.VISIBLE);
                image1.setImageBitmap(thumbnail_1);
                imageTag = 1 ;
                saveImage(resized, (int) System.currentTimeMillis());
                img1 = false ;
                imgDelete1.setVisibility(View.VISIBLE);
            }
}}

my function to save Image is :

public void saveImage(Bitmap bitmap, int i){
    try {

        String stored = null;

        File sdCard = Environment.getExternalStorageDirectory();
        File file = new File(sdCard, "/GMC/Images"
                + File.separator + user_id + "_" + imageTag + ".jpg");

        if (file.exists()) {
            file.delete();
        }



        if (isWriteStorageAllowed()) {
            if ( isReadStorageAllowed())
            //file.createNewFile();
            try {

                FileOutputStream out = new FileOutputStream(file);//getting exception at this line
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.flush();
                out.close();
                stored = "success";
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (imageTag == 1) {
                selectedImagePath1 = Environment.getExternalStorageDirectory() + "/GMC/Images"
                        + File.separator + user_id + "_" + imageTag + ".jpg";
            } 
        }
        else {
            requestWriteStoragePermission();
            requestStoragePermission();
        }

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

already added permissions in manifest like

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

also checked runtime permissions :

//We are calling this method to check the permission status
private boolean isReadStorageAllowed() {
    //Getting the permission status
    int result = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);

    //If permission is granted returning true
    if (result == PackageManager.PERMISSION_GRANTED)
        return true;
    else {
        //If permission is not granted returning false
        return false;}
}

private boolean isWriteStorageAllowed() {
    //Getting the permission status
    int result = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    //If permission is granted returning true
    if (result == PackageManager.PERMISSION_GRANTED)
        return true;
    else {
        //If permission is not granted returning false
        return false;}
}

//Requesting permission
private void requestStoragePermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.READ_EXTERNAL_STORAGE)){
        //If the user has denied the permission previously your code will come to this block
        //Here you can explain why you need this permission
        //Explain here why you need this permission
    }

    //And finally ask for the permission
    ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},PERMISSION_READ_EXTERNAL_STORAGE);
}

//Requesting permission
private void requestWriteStoragePermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.WRITE_EXTERNAL_STORAGE)){
        //If the user has denied the permission previously your code will come to this block
        //Here you can explain why you need this permission
        //Explain here why you need this permission
    }

    //And finally ask for the permission
    ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},PERMISSION_WRITE_EXTERNAL_STORAGE);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    {
        switch (requestCode){
            case PERMISSION_READ_EXTERNAL_STORAGE :
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(context , "Read Permission granted",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(context , "Read Permission Denied" ,Toast.LENGTH_SHORT).show();
                }
                return;
            case  PERMISSION_WRITE_EXTERNAL_STORAGE :
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(context , "Write Permission granted" ,Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(context , "write Permission Denied",Toast.LENGTH_SHORT).show();
                }
                return;
        }

    }
}

Checked the link java.io.FileNotFoundException: /storage/emulated/0/saved_images/grub.jpg: open failed: ENOENT (No such file or directory)

I tested the code on two different phone one is lenovo(Marshmallow) and other is samsung(marshmallow) but it gives error in samsung where as works perfectly on lenovo.

what will be the issue? please help

Pranita
  • 803
  • 7
  • 16
  • Please provide the complete Java stack trace, along with your code that is referenced in that stack trace. Bear in mind that you do not write out the file if you do not have permission. While you request permission in that case, you do not circle back and save the image. I strongly recommend that you request the permissions before starting the `ACTION_IMAGE_CAPTURE` `Intent`. – CommonsWare Dec 05 '17 at 12:55
  • @Ronak.. folders are not created...what should i do now – Pranita Dec 05 '17 at 13:57
  • @CommonWare.. I requested permission in click event also.i.e. before ACTION_IMAGE_CAPTURE – Pranita Dec 05 '17 at 13:58
  • @RonakThakkar all the images are going into default camera folder – Pranita Dec 05 '17 at 14:01
  • `folders are not created...what should i do now`, Then who should have done that? There is no code that creates those folders. Nor are you checking if those folders exist before trying to put a file in it. Create the folders if they do not exist! – greenapps Dec 05 '17 at 14:10
  • `all the images are going into default camera folder ` No. Not at all. Thats not the default camera folder. – greenapps Dec 05 '17 at 14:11

1 Answers1

-1

Try adding largeheap=true in your manifest file.Lenovo phones cannot get large images whereas samsung phones are able to get large image from gallery camera etc.

Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18