0

I've been trying to access the phone's storage. I put in my manifest:

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

I also asked for permissions in my mainactivity & gave the application the required permissions.

Although the app has permissions, I receive this error while running my code:

I/System.out: /storage/emulated/0/Music
W/System.err: com.sapher.youtubedl.YoutubeDLException: Cannot run program "youtube-dl" (in directory "/storage/emulated/0/Music"): error=13, Permission denied
W/System.err:     at com.sapher.youtubedl.YoutubeDL.execute(YoutubeDL.java:69)
                  at net.musify.liel.musify.MainActivity.onCreate(MainActivity.java:40)
                  at android.app.Activity.performCreate(Activity.java:7183)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
                  at android.os.Handler.dispatchMessage(Handler.java:105)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6938)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Any ideas?

Edit:

private boolean permissions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkPermissions();

        if(permissions) {
            String videoUrl = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";

            System.out.println(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC));
            String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();

            YoutubeDLRequest request = new YoutubeDLRequest(videoUrl, directory);
            request.setOption("ignore-errors");     // --ignore-errors
            request.setOption("output", "%(id)s");  // --output "%(id)s"
            request.setOption("retries", 10);       // --retries 10

            // Make request and return response
            try {
                YoutubeDLResponse response = YoutubeDL.execute(request);
                String stdOut = response.getOut(); // Executable output
                System.out.println(stdOut);
            } catch(Exception e) {
                e.printStackTrace();
            }
            //Downloader.download("2FFdYZ1ZsbA");
        }
    }

    public void checkPermissions() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions();
        } else {
            permissions = true;
        }
    }


    private final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1;
    public void requestPermissions() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_CONTACTS)) {
                requestPermissions();
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.INTERNET},
                        MY_PERMISSIONS_REQUEST_READ_CONTACTS);
            }
        } else {
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    this.permissions = true;
                } else {
                    this.permissions = false;
                    Toast.makeText(this, "Musify needs permissions in order to run properly!", Toast.LENGTH_SHORT).show();;
                    System.exit(0);
                }
                return;
            }

            // other 'cases' to check for other permissions this app might request.
    }
}

This is the updated code with the runtime permission request.

Liel Amar
  • 45
  • 11

1 Answers1

0

You need to check the runtime permission for storage like this

public void checkPermissions() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }
}

and in your Result callback

    @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
        //resume tasks needing this permission
    }
}
Anand
  • 1,866
  • 3
  • 26
  • 49