简体   繁体   中英

Permission error when getting file for use with JSch

I am trying to make an app that allows me to ssh and do certain commands.

Using JSch, I wrote an AsyncTask as such that takes care of the process. The issue is that I cannot access the .pub file for RSA logins due to the error java.io.FileNotFoundException: /storage/emulated/0/MyFolder/id_rsa.pub: open failed: EACCES (Permission denied) . On my Nexus 6, "MyFolder" folder exists, along with the .pub file. Permissions are the same as other files in Download.

SomeAsyncTask.java

import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.File;

public class SomeAsyncTask extends AsyncTask<String, String, String> {
    public static final String TAG = "SomeAsyncTask";
    @Override
    protected String doInBackground(String... params) {
        Log.d(TAG, params[0]);
        this.submit(params[0]);
        return null;
    }

    private void submit(String command) {
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession("user", "ip", 22);
            File file = new     File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyFolder/", "id_rsa.pub");
            jsch.addIdentity(file.toString());
            session.connect();
        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        }
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.whatever.whate">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <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" />
</manifest>

I believe I have all the permissions and the folder/file exists. What could be causing this?

Permission error when getting file for use with JSch

Always remember when running on an Android Marshmallow device that the permissions systems way of working has changed.

http://developer.android.com/training/permissions/requesting.html

You need to make sure you are taking care of runtime permissions correctly, and if not make sure you allow all the permissions on your device from the settings application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM