0

I have the next method :

public void saveOnSDCard() {
    File path = new File("storage/sdcard1");

    File dir = new File(path + "");
    if (!dir.exists()) {
        dir.mkdirs();
    }

    File file = new File(dir, "test.png");

    OutputStream os;
    try {
        os = new FileOutputStream(file);
        image.compress(Bitmap.CompressFormat.PNG, 100, os);
        os.flush();
        os.close();
    } catch (IOException ioe) {
        showToast("Ошибка " + ioe.toString());
    }
}

but when i try to call it, it gives me a java.io.fileNotFoundException(Permission denied). I have the next permission in my Manifest file:

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

So can someone tell me what's wrong ?

P.S image - Bitmap object, i declared it in another method.

2 Answers2

0

try if one of the following solutions works:

  1. replace "storage/sdcard1" with"storage/sdcard1/"
  2. replace path + "" with path + "/"
  3. replace "test.png" with "/test.png"

I think it's just missing a slash in one of those three lines

Giulio Pettenuzzo
  • 786
  • 1
  • 5
  • 20
0

Guy in comment told me that if you have an android 4.4+ you can write to sdcard only when you make your sdcard as primary memory or sometimes when ur phone is rooted. I tried it and it really works.