Permission error when getting file for use with JSch - java

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.

Related

What does 'No file known for: classes2.dex' mean?

I built an app and I have different classes, and first I got an error like this when I tried to run the app
Cannot fit requested classes in a single dex file (# methods: 66087 > 65536)
I followed some instructions from Google and I added to my build.gradle
this dependency
implementation 'com.android.support:multidex:1.0.3'
and to the defaultConfig this
multiDexEnabled true
In my manifest xml I added this
android:name="android.support.multidex.MultiDexApplication"
but its highlighted with red color and when I move the cursor over it says
Unresolved class MultiDexApplication
When I try to run the app again it throws me
No file known for: classes2.dex
Known maps: {RelativeFile{base=/Users/***/AndroidStudioProjects/****/app/build/intermediates/dex/debug/mergeDexDebug/out/classes.dex, path=classes.dex, type=DIRECTORY}=classes.dex, RelativeFile{base=/Users/****/AndroidStudioProjects/****/app/build/intermediates/dex/debug/mergeProjectDexDebug/out/classes.dex, path=classes.dex, type=DIRECTORY}=classes3.dex, RelativeFile{base=/Users/*****/AndroidStudioProjects/****/app/build/intermediates/dex/debug/mergeExtDexDebug/out/classes2.dex, path=classes2.dex, type=DIRECTORY}=classes4.dex}
This is the MainActivity Class
package com.example.drivieapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent i = new Intent(this, screenNotLogged.class );
Thread timer = new Thread() {
public void run() {
try {
sleep(3100);
}
catch(InterruptedException e) {
e.printStackTrace();
}
finally {
startActivity(i);
finish();
}
}
};
timer.start();
}
}
Can you please explain me what I have to do? I google the solution but didn't understand too much. Thank you very much!
Manifest xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="****">
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Drivie"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:name="android.support.multidex.MultiDexApplication"
>
<activity android:name=".SignupCarrier"></activity>
<activity android:name=".carrierSignupHint" />
<activity android:name=".home" />
<activity android:name=".SignupSender" />
<activity android:name=".signup" />
<activity android:name=".screenNotLogged" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

BroadcastReceiver for Incoming calls in android doesn't work

I'm trying to develop android app for that can record phone calls. So, in the initial step, I've to see if BroadcastReceiver is getting fired or not.
I've added permissions, receiver tag in AndroidManifest file. I'm testing on OnePlus X. Activity is gets started but BroadcastReceiver doesn't get fired when I get call. What's going wrong here?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="com.example.myapp.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
PhoneStateReceiver.Java
package com.example.myapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class PhoneStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
Toast.makeText(context,"Incoming Call State",Toast.LENGTH_SHORT).show();
Toast.makeText(context,"Ringing State Number is -"+incomingNumber,Toast.LENGTH_SHORT).show();
}
if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){
Toast.makeText(context,"Call Received State",Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
Toast.makeText(context,"Call Idle State",Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
The permission READ_PHONE_STATE is a dangerous permission, if you are on a marshmallow device you must request runtime permission else your broadcast receiver will not work neither it will throw an error.
That is the most likely the cause of issue from your code because you have correctly registered in the Intent filter other than that there is nothing wrong as it is just a broadcast receiver and should work, i.e get called by the Android system.

MapFragment won't load

I'm just trying to see a map in my android app and i'm having some troubles.
I've been following severals tutorial and still a have no result.
I downloaded google play services from my sdk manager and imported google_play_services_lib correctly i guess.
So i created a Google Maps API Key from Google Api Console using my packagename and my SHA1, which i found using cmd and also in eclipse in window>preferences>android>build, i got the same SHA1 in both ways so i think it's correct.
So i set my manifest like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapprova"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<permission android:name="com.example.googlemapprova.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.googlemapprova.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my api key" />
<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" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
in my layout file i created just a map fragment
<fragment
android:id="#+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/textView1"
android:layout_centerHorizontal="true" />
I've changed android:name with class="com.google.android.gms.maps.SupportMapFragment" because of a tutorial
And then my main
package com.example.googlemapprova;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity {
GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
initializeUiSettings();
initializeMapLocationSettings();
initializeMapTraffic();
initializeMapType();
initializeMapViewSettings();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected void onResume() {
super.onResume();
// initilizeMap();
}
private void initilizeMap() {
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
public void initializeUiSettings() {
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(false);
googleMap.getUiSettings().setTiltGesturesEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
}
public void initializeMapLocationSettings() {
googleMap.setMyLocationEnabled(true);
}
public void initializeMapTraffic() {
googleMap.setTrafficEnabled(true);
}
public void initializeMapType() {
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
public void initializeMapViewSettings() {
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(false);
}
}
i got the code in my main from a tutorial and modified it just a little, in this way i managed my app to not crash(if i don't use this code my app will crash).
this is what i get
i'm using a motorola moto g3 phone to try it
So i tryed to download aLogcat on my phone from play store, but i get nothing on it, maybe i'm using it in the wrong way.
this is the tutorial i used the most
Can someone help me to view a map on my app? I can't understand why it's not working
I'm trying to get the Logcat now
Your map fragment is loaded, but map content does not.
Where you placed your API key?
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my api key" />

android: mkdirs() returns false

Can someone tell me, why my app can't create dirs?
Here is my Code:
package com.example.android.myapplication;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Environment;
import java.io.File;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + "meinordner");
if(!dir.exists()) {
boolean s = dir.mkdirs();
if(!s) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("could not create dir");
alert.show();
}
}
}
}
and my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.myapplication" >
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
when i execute the app, the alert is shown. I even tried to start the app when my phone is not connected to the computer and therfore the storage is not mounted somewhere else
First, change:
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>
to:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Second, change:
new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + "meinordner");
to:
new File(Environment.getExternalStorageDirectory(), "meinordner");

Cannot create folder in SD card on Android

I have this problem in my application. I want to create a folder in Emulator SD card but it didn't work and I don't know why. I tried mkdir and mkdirs.
I added WRITE_EXTERNAL_STORAGE Permission to the Manifest. I tried with MediaScannerConnection and didn't work. I've searched a lot the web and tried a lot of implementations but no change. What could be the issue?
Here is my code:
public void createDirIfNotExists() {
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/cdp");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
Log.e("Folder: ", "folder created!!");
} else {
Log.e("Folder Error: ", "folder cannot be created!!");
}
}
My manifest look like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aboussof.myapplication" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
<activity android:name=".Test">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And so I try to create the folder:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressDialog = new ProgressDialog(Test.this);
mProgressDialog.setMessage("A message");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
// execute this when the downloader must be fired
final DownloadTask downloadTask = new DownloadTask(Test.this);
createDirIfNotExists();
downloadTask.execute("http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_5mb.mp4");
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
downloadTask.cancel(true);
}
});
}
Replace
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/cdp");
with
File folder = new File(Environment.getExternalStorageDirectory() + "/", "cdp");
Have you solved your issue yet?
I use your method with a tiny change at
File folder = new File(Environment.getExternalStorageDirectory(), "cdp");
And the result like the following screenshot:
My AVD configuration like the following:
I have had the same issue - file.mkdirs() returns false on android emulator API 23. And all configs was set according to BNK's answer.
Try to change API to 19 - it works for me. Looks like weird emulator/android version issue.

Categories

Resources