0

In my Android project i'm saving an image file into the device. in there I use the following code for creating file.

File folder = new File(getFilesDir());
        if (!folder.exists()) {
            folder.mkdir();
        }
File imageFile = new File(folder, filename);
try
  {
  FileOutputStream fos = new FileOutputStream(imageFile);
  bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
  fos.close();
  }
  catch (Exception e)
  {
    e.printStackTrace();
  }

but this gives me File not found exception when creating FileOutputStream

FileOutputStream fos = new FileOutputStream(imageFile);

so the file seems not creating. how can I fix this.

Vodet
  • 1,491
  • 1
  • 18
  • 36
Samantha Withanage
  • 3,811
  • 10
  • 30
  • 59

3 Answers3

0

I think the folder wasn't created. If the folder has a path like this one: /folder1/folder2. Could you try to create the folder1 first then create the folder2 ?. After that, you run your code again.

John Le
  • 1,116
  • 9
  • 12
  • I'm creating only one folder "PAssets". my file path when debugging is as following. */storage/emulated/0/PAssets/PProfilePicture.png* – Samantha Withanage Sep 12 '17 at 10:05
  • Could you post your logs and show me the `getFilesDir()` method ? – John Le Sep 12 '17 at 10:08
  • private String getFilesDir() { return Environment.getExternalStorageDirectory() + ASSETS_FOLDER_NAME; } – Samantha Withanage Sep 12 '17 at 10:10
  • I did like you and it works fine. I don't know there is a '/' in the `ASSETS_FOLDER_NAME` constant :final String downloadPath = Environment.getExternalStorageDirectory().toString() + "/PAssets"; File folder = new File(downloadPath); if (!folder.exists()) { boolean value = folder.mkdir(); Log.d(TAG, "Create dir: " + value); } File imageFile = new File(folder, "PProfilePicture.png"); – John Le Sep 12 '17 at 10:21
  • If you use Android 6 or higher. Could you check `Storage` permission is enable in the app setting ? If it is not enable then enable it and try again. See this link to check that: https://android.gadgethacks.com/how-to/android-basics-manage-app-permissions-marshmallow-higher-0168220/ – John Le Sep 12 '17 at 10:36
0

You need to explicitly create a file as following :

 File imageFile = new File(folder, filename);

 if(!imageFile.exists()){
    imageFile.createNewFile();
 }
iMDroid
  • 2,108
  • 1
  • 16
  • 29
0

The issue was I'm using an Android emulator running Android 6.0 and I'm not checking runtime permissions. Prior to API 23 when accessing external storage we need runtime permissions. I checked the permissions as below.

public  boolean isStoragePermissionGranted() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                return true;
            } else {
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                return false;
            }
        }
        else { 
            return true;
        }
    }

an once permission granted it received through following callback so that we can perform our task.

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
            saveTempImage(bmp, filename);
        }
    }

Thanks all for kind help!

Samantha Withanage
  • 3,811
  • 10
  • 30
  • 59
  • 1
    Nonsense. `getFilesDir()` is internal memory and you do not need any permission for it. `the file is /storage/emulated/0/PAssets/PProfilePicture.png` Impossible as `/storage/emulated/0` is not internal memory but external. Your code does not represent your problem. – greenapps Sep 12 '17 at 12:15