Johny strawits
Johny strawits

Reputation: 55

How to get permission in android app?

I have problem with getting permission to save file from raw on phone. I have in manifest this:

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

In java where I'm using onclick I have function to save file

saveTestButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

                InputStream in = null;
                FileOutputStream fout = null;
                try {
                    in = getResources().openRawResource(R.raw.testsound);
                    String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
                    String filename = "testsound.mp3";
                    fout = new FileOutputStream(new File(downloadsDirectoryPath + filename));

                    final byte data[] = new byte[1024];
                    int count;
                    while ((count = in.read(data, 0, 1024)) != -1) {
                        fout.write(data, 0, count);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fout != null) {
                        try {
                            fout.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
        }

    });

And that to check permission:

private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

And when I try to save this file by click the button in app I'm getting error:

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Downloadtestsound.mp3: open failed: EACCES (Permission denied)

What is wrong?

Upvotes: 0

Views: 351

Answers (2)

Divyesh Kalotra
Divyesh Kalotra

Reputation: 204

put "/" between filename and downloadsDirectoryPath that's why it giving error .for testing if you don't get permissions then go to settings and check there manually grant permission and launch again.

public void onClick(View v) {

        InputStream in = null;
        FileOutputStream fout = null;
        try {
            in = getResources().openRawResource(R.raw.testsound);
            String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
            String filename = "testsound.mp3";
            fout = new FileOutputStream(new File(downloadsDirectoryPath + "/"+filename));

            final byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data, 0, 1024)) != -1) {
                fout.write(data, 0, count);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fout != null) {
                try {
                    fout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
}

});

Upvotes: 1

Henry
Henry

Reputation: 17841

I believe this is your problem:

fout = new FileOutputStream(new File(downloadsDirectoryPath + filename));

Use the below instead:

fout = new FileOutputStream(new File(downloadsDirectoryPath , filename));

You have permission only to /storage/emulated/0/Download/testsound.mp3 and not /storage/emulated/0/Downloadtestsound.mp3

Upvotes: 1

Related Questions