I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource:
file:///android_asset/webkit/android-weberror.png", source:
data:text/html,chromewebdata (0) I found many methods to solve
it,like:
webView.loadUrl("file:///android_asset/assets/wwww/js/mypage.html");
webView.loadUrl("file:///assets/mypage.html");
webView.loadUrl("file:///android_asset/mypage.html");
All of them can not work.
Be sure that your file mypage.html really exists inside /assets directory:
webView.loadUrl("file:///android_asset/mypage.html");
you need to declare the permission into your AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Using Android 6.0+ must request WRITE_EXTERNAL_STORAGE permissions manually:
private void checkExternalStoragePermission() {
int permissionCheck = ContextCompat.checkSelfPermission(
this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
Log.i("Message", "PERMISSION NOT SET.");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 225);
} else {
Log.i("Message", "Permission OK!");
}
}
Related
I am trying to use a simple Flutter plugin (speech recognition wrapper) and have no idea how to request the appropriate permissions on Android 23 or newer. In the Dart part I have:
Future requestPermissions() =>
_channel.invokeMethod("speech.requestPermissions");
In the Android part:
public class SpeechRecognitionPlugin implements MethodCallHandler, RecognitionListener,
PluginRegistry.RequestPermissionResultListener {
Plugin registration:
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "speech_recognition");
SpeechRecognitionPlugin speechRecognitionPlugin = new
SpeechRecognitionPlugin(registrar.activity(), channel);
channel.setMethodCallHandler(speechRecognitionPlugin);
registrar.addRequestPermissionResultListener(speechRecognitionPlugin);
}
Method call:
else if (call.method.equals("speech.requestPermissions")) {
Log.d(LOG_TAG, "speech.requestPermissions");
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.RECORD_AUDIO)) {
Toast.makeText(activity.getApplicationContext(), "This application needs the Record Audio permission for recognition to work", Toast.LENGTH_LONG).show();
} else {
Log.d(LOG_TAG, "Requesting permissions");
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.RECORD_AUDIO},
1);
}
result.success(hasRecordAudioPermission());
Result callback:
#Override
public boolean onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
boolean granted = false;
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
granted = true;
}
speechChannel.invokeMethod("speech.onPermission", granted);
return true;
}
}
return false;
}
From logcat I see that the "speech.requestPermissions" call happens, but standard Android system permission request is not shown, just this in the logcat may be related:
D/ViewRootImpl(21171): #1 mView = android.widget.LinearLayout{64f050b
V.E...... ......I. 0,0-0,0 #102039d android:id/toast_layout_root}
D/ViewRootImpl(21171): MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
D/ViewRootImpl(21171): #3 mView = null
What is the correct way to request permissions for Flutter plugins?
EDIT: This does not apply to the first run, when the dialog shows correctly, but to subsequent runs when the user did not grant the permission at first or revoked it via settings. I realize that changes the question significantly (making it appear as edge case), but Android permissions are not supposed to work that way.
EDIT: The permissions are present in AndroidManifest.xml
Use Permission plugin for flutter
Request permission
import 'package:permissions_plugin/permissions_plugin.dart';
Map<Permission, PermissionState> permission = await PermissionsPlugin
.requestPermissions([
Permission.ACCESS_FINE_LOCATION,
Permission.ACCESS_COARSE_LOCATION,
Permission.READ_PHONE_STATE
]);
Check status permission
import 'package:permissions_plugin/permissions_plugin.dart';
Map<Permission, PermissionState> permission = await PermissionsPlugin
.checkPermissions([
Permission.ACCESS_FINE_LOCATION,
Permission.ACCESS_COARSE_LOCATION,
Permission.READ_PHONE_STATE
]);
I have this working for location permissions. The only thing I'm doing differently is in your method call here:
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.RECORD_AUDIO},
1);
Instead of using 'ActivityCompat' I store the registrar in a local final variable and I'm doing the following :
registrar.activity().requestPermissions(activity,
new String[]{Manifest.permission.RECORD_AUDIO},
1);
EDIT: Also make sure that you have included the relevant permissions in your AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Add this -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- Flutter stuff --->
</manifest>
Let's say you want to request camera permission using permission_handler package.
In pubspec.yaml file:
permission_handler: ^8.0.0+2
(For Android) Add the permission to android/app/src/main/AndroidManifest.xml file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
...
</manifest>
(For iOS),
(i) Add this to your info.plist file
<key>NSCameraUsageDescription</key>
<string>App needs camera permission to work</string>
(ii) Add 'PERMISSION_CAMERA=1' to your Podfile.
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
## Add the following line.
'PERMISSION_CAMERA=1'
]
end
end
end
Request the permission:
final status = await Permission.camera.request();
if (status == PermissionStatus.granted) {
print('Permission granted');
} else if (status == PermissionStatus.denied) {
print('Permission denied. Show a dialog and again ask for the permission');
} else if (status == PermissionStatus.permanentlyDenied) {
print('Take the user to the settings page.');
await openAppSettings();
}
I have the following code in my MainActivity.
Log.d("FILES", Environment.getExternalStorageDirectory().getAbsolutePath());
Log.d("FILES", Environment.getRootDirectory().getAbsolutePath());
Log.d("FILES", Arrays.toString(Environment.getRootDirectory().list()));
Log.d("FILES", Arrays.toString(Environment.getExternalStorageDirectory().list()));
Log.d("FILES", Environment.getExternalStorageState());
Log.d("FILES", String.valueOf(Environment.getExternalStorageDirectory().listFiles() != null));
The aboce code shows an output like this in logcat:
12-28 08:38:51.344 23429-23429/com.tahsinalsayeed.codeviewer D/FILES: /storage/emulated/0
12-28 08:38:51.344 23429-23429/com.tahsinalsayeed.codeviewer D/FILES: /system
12-28 08:38:51.344 23429-23429/com.tahsinalsayeed.codeviewer D/FILES: [lost+found, app, bin, build.prop, data, etc, fonts, framework, lib, media, plugin, preloadres, priv-app, recovery-from-boot.p, res, tts, usr, vendor, xbin]
12-28 08:38:51.347 23429-23429/com.tahsinalsayeed.codeviewer D/FILES: null
12-28 08:38:51.353 23429-23429/com.tahsinalsayeed.codeviewer D/FILES: mounted
12-28 08:38:51.356 23429-23429/com.tahsinalsayeed.codeviewer D/FILES: false
I can't make heads or tails why I can't list files under getExternalStorageDir. According to the accepted answer to this question this should work. I have the following permissions in my manifest.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I have tested in on both physical device and emulator with API 23.
In Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.
Note: Mention permission in manifest will not work in API 23, you need to ask permission during run time.
Please follow this official link here
askForPermission(Manifest.permission.READ_EXTERNAL_STORAGE,100);
Method to ask permission from user
private void askForPermission(String permission, Integer requestCode) {
if (ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) {
//This is called if user has denied the permission before
//In this case I am just asking the permission again
ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
}
} else {
Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
}
}
To handle the results of a permission request, the onRequestPermissionsResult method is called. It’s code is below:
Note: When a result is returned, it checks whether the permission was granted or not. If it is, the requestCode is passed.
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED) {
//permission granted
} else {
//Permission Denied
}
}
I am writing an app to send msg over TCP Socket. I am struggling to get access to the internet permission. (API 26 is the target)
socket EACCESS permission denied
I have setup in my Manfest :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.seb.sebastien.opengardenirc">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.persmission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
As I am using the API26, I have to add a permission check in the code to make sure it works fine:
if (shouldAskPermission()) {
Log.d(TAG, "Permission is required");
String[] PERMISSIONS_STORAGE = {
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_WIFI_STATE
};
int permission_internet = ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET);
int permission_network = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE);
int permission_wifi = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE);
Log.d(TAG, "Package Manager: " + PackageManager.PERMISSION_GRANTED);
Log.d(TAG, "INTERNET : " + permission_internet);
Log.d(TAG, "NETWORK : " + permission_network);
Log.d(TAG, "WIFI : " + permission_wifi);
if ((permission_internet != PackageManager.PERMISSION_GRANTED)
|| (permission_network != PackageManager.PERMISSION_GRANTED)
|| (permission_wifi != PackageManager.PERMISSION_GRANTED)){
Log.d(TAG, "Asking for permission");
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this,
PERMISSIONS_STORAGE,
REQUEST_CONNECTION_PERMISSION
);
} else {
Toast.makeText(MainActivity.this, "Permission (already) Granted!", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Permission already granted");
startTcpInAThread("str");
}
}
And I have also the method to catch the onRequestPermissionsResult.
private boolean shouldAskPermission(){
return(Build.VERSION.SDK_INT> Build.VERSION_CODES.LOLLIPOP_MR1);
}
#Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){
switch(permsRequestCode){
case REQUEST_CONNECTION_PERMISSION:
Log.d(TAG, "Permission granted result : " + grantResults[0]);
Log.d(TAG, "Permission granted result : " + grantResults[1]);
Log.d(TAG, "Permission granted result : " + grantResults[2]);
boolean accepted = (grantResults[0]== PackageManager.PERMISSION_GRANTED)
&& (grantResults[1]== PackageManager.PERMISSION_GRANTED)
&& (grantResults[2]== PackageManager.PERMISSION_GRANTED);
if(accepted) {
startTcpInAThread("str");
} else {
Toast.makeText(getApplicationContext(), "Permissions issues!!!!", Toast.LENGTH_SHORT).show();
}
break;
}
}
First things is that when checking the permission, I can see that android.persmission.INTERNETis not granted. Good thing is that it's going to call the request permission method ActivityCompat.requestPermissions
I was expecting a Dialog box showing up but my understanding is that the dlgbox only show up for some on the permission requests some other do not need to ask.
My concerns is that when I got onRequestPermissionsResultthe permission is still not granted.
Anybody know how to grant the permission to internet ?
I have also pay attention to not run the network access in the same thread.
Regards
I am trying to copy some files from remotely connected pc to my Android Device's SDCard but it is showing an error "No write permission". while i transfer same files to internal storage, it transferred successfully.
I have already included these permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
My Android Device is running on Android Version 6.0.1
Use Run time permission for Android Version 6.0.0 and more
Click here for more information
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v("======>", "Permission is granted");
return true;
} else {
Log.v("======>", "Permission is revoked");
//1 is request code
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
} else { //permission is automatically granted on sdk<23 upon installation
Log.v("======>", "Permission is granted");
return true;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
//1 is request code
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.v("======>", "Permission: " + permissions[0] + "was " + grantResults[0]);
openFileAttachDialog();
}
return;
}
}
I want to get all granted permissions. I know I can get all requested permissions by packageinfo.requestedPermissions but I want to know list of granted permissions and granted permissions can be lesser then requested in case of android M. So I just wanted to know that is there way that I can get list of all granted permissions?
I know from list of requested permission I can check for that permission weather granted or not but I want to know list of all granted permission. Don't want to check for every requested permission.
A simple function that returns all the permissions that have been requested and granted for a given package could look like this:
List<String> getGrantedPermissions(final String appPackage) {
List<String> granted = new ArrayList<String>();
try {
PackageInfo pi = getPackageManager().getPackageInfo(appPackage, PackageManager.GET_PERMISSIONS);
for (int i = 0; i < pi.requestedPermissions.length; i++) {
if ((pi.requestedPermissionsFlags[i] & PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0) {
granted.add(pi.requestedPermissions[i]);
}
}
} catch (Exception e) {
}
return granted;
}
Note that this requires API level 16 or above, but that should hopefully not be an issue these days.
You can check permission one by one and add to list:
// Should we show an explanation?
List<String> listPermissionsNeeded = new ArrayList<>();
// No explanation needed, we can request the permission.
if((ContextCompat.checkSelfPermission(context,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED))
{
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if((ContextCompat.checkSelfPermission(context,
Manifest.permission.GET_ACCOUNTS)
!= PackageManager.PERMISSION_GRANTED))
{
listPermissionsNeeded.add(Manifest.permission.GET_ACCOUNTS);
}
if((ContextCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED))
{
listPermissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if((ContextCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED))
{
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if((ContextCompat.checkSelfPermission(context,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED))
{
listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(context,
listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
MY_PERMISSIONS_REQUEST_MULTIPLE_PERMISSION);
}