My project use the Android Google Map, hoping to display the positioning blue dot at the current position 。But whether I use all methods, the project cannot show the current blue dot.
I hope to use the blue dot that comes with Google instead of the mark drawn by myself。
show code:
step1:
MapsInitializer.initialize(context, MapsInitializer.Renderer.LATEST, new OnMapsSdkInitializedCallback() {
#Override
public void onMapsSdkInitialized(#NonNull MapsInitializer.Renderer renderer) {
switch (renderer) {
case LATEST:
WriteStreamAppend.method1("google onMapsSdkInitialized LATEST ");
break;
case LEGACY:
WriteStreamAppend.method1("google onMapsSdkInitialized LEGACY ");
break;
}
}
});
step2:
mMapViewLayout = (MapView) mapView;
mMapViewLayout.onCreate(savedInstanceState);
mMapViewLayout.getMapAsync(this);
step3: when onMapLoaded run
if (ActivityCompat.checkSelfPermission(mainActivity, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(mainActivity, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mGoogleClient.setMyLocationEnabled(needLocationEnable);// true
UiSettings uiSettings = mGoogleClient.getUiSettings();
if (uiSettings != null) {
uiSettings.setMyLocationButtonEnabled(false);
}
} else {
PermissionUtils.requestPermissions(mainActivity);
}
Runtime Environment:
implementation 'com.google.android.gms:play-services-maps:18.1.0'
implementation 'com.google.android.gms:play-services-location:19.0.1'
...
compileSdkVersion = 31
in build.gradle
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
in manifest.xml
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN
in permisstion request
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
in setting.gradle
at android version 12
app screenshot:
enter image description here
If you can help me, be grateful!Thanks!
I hope to use the blue dot that comes with Google instead of the mark drawn by myself。Because I can display the blue dot normally in the other project, but I really can't find the difference
Related
I am working on a simple camera app whose pictures should only be visible in the app that created the pictures. I am testing with Samsung galaxy phone running on android 12. My snippets are in kotlin
I want to save to a directory that does not get scanned for media. However, I think I might have a permission problem trying to access the data directory such as "/data/user/0/com.example.demo.camerax/files/CameraXDemo/".
Whenever I attempt to save pictures, I get the error "Failed to write destination file"
// Mirror image when using the front camera
isReversedHorizontal = lensFacing == CameraSelector.DEFAULT_FRONT_CAMERA
}
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
// Options fot the output image file
val outputOptions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val extn = "beg"
val filename = System.currentTimeMillis()
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, "$filename")
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
put(MediaStore.MediaColumns.RELATIVE_PATH, oDir)
}
val contentResolver = requireContext().contentResolver
// Create the output uri
val contentUri = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
OutputFileOptions.Builder(contentResolver, contentUri, contentValues)
} else {
File(oDir).mkdirs()
val file = File(oDir, "${System.currentTimeMillis()}.jpg")
OutputFileOptions.Builder(file)
}.setMetadata(metadata).build()
This is the snippet that should create the directory above and save the pictures.
I have set my permissions in the Manifest file as follows
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
Android Marshmallow requires that the user will grant permission in order to make modifications. Here's how I implemented this
private val permissions = mutableListOf(
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
).apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
add(Manifest.permission.ACCESS_MEDIA_LOCATION)
}
}
private val permissionRequest = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
if (permissions.all { it.value }) {
onPermissionGranted()
} else {
view?.let { v ->
Snackbar.make(v, R.string.message_no_permissions, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.label_ok) { ActivityCompat.finishAffinity(requireActivity()) }
.show()
}
}
}```
Is there a better approach to hiding pictures taken
I'm trying to create a camera picker functionality on the app and I face some of the methods of camera picture is deprecated so I trying to resolve the code and not using any depricated method.
First on manifest declares the permissions
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
after that create a Camera intent:
private var imageStr:Uri?=null
val values = ContentValues()
values.put(Media.TITLE, "New Picture")
values.put(Media.DESCRIPTION, "From the Camera")
imageStr = requireActivity().contentResolver.insert(EXTERNAL_CONTENT_URI, values)
// camera intent
val intent = Intent(ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageStr)
startForCameraResult.launch(intent)
and after that we get the value on startForCameraResult:
private val startForCameraResult = registerForActivityResult(StartActivityForResult()) {
if (it.resultCode == RESULT_OK) {
profileImage.setImageURI(imageStr)
}
}
Here is the code And The problem is I always get imagestr is null.
Please show me the right direction to handle this issue.
Thanks
If you are using targetSdkVersion 29 & compileSdkVersion 29 you need to set android:requestLegacyExternalStorage="true" in application tag of AndroidManifest.xml file.
I tried to make some folder in my storage and store image from camera into it, but mkdirs always return false, already tried using mkdir and canWrite, but still the same
2020-02-12 09:32:44.043 22914-23146/example.com.absensiapp D/TET: Folder Not Exist
2020-02-12 09:32:44.047 22914-23146/example.com.absensiapp D/TET: Failed To Create Directory
Here is the code for making the folder and save the image from camera
String wholeFolderPath = fh.TRAINING_PATH + name;
File dir = new File(wholeFolderPath);
if(!dir.exists()) {
Log.d("TET", "Folder Not Exist");
//create new directory
if(dir.mkdirs())
fh.saveMatToImage(m, wholeFolderPath + "/");
else
Log.d("TET", "Failed To Create Directory");
}
else {
Log.d("TET", "Folder Exist");
//save the image to directory
fh.saveMatToImage(m, wholeFolderPath + "/");
}
Already put the permission in my manifest and main activity
Manifest :
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Main Activity :
private static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
private void checkPermission() {
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
};
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
}
Solve the problem. Downgrade my targetSdkVersion in build.gradle from 29 to 21
I'm trying to request the ability to read contacts in an app, and have followed several tutorials. All of these use nearly the same code for this process. Below is the code in my MainActivity.java file, that should request permission.
private void checkContactPermissions()
{
if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Contacts permission NOT granted");
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}
else
{
Log.i(TAG, "Contacts permission granted");
readContacts();
}
}
My manifest.xml also includes the line:
<uses-permission android:name="android.permission.READ_CONTACTS" />
When the app is run, either on emulator or physical debugging device, it does not ask for permission, however the log states that the permission was granted. I have confirmed the permission is off by going to the settings and checking it was turned off. What else would be causing the app to perform as if permissions were granted.
Try this,
private Context mContext=YourActivity.this;
private static final int REQUEST = 112;
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.READ_CONTACTS};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
} else {
readContacts();
}
} else {
readContacts();
}
get Permissions Result
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
readContacts();
} else {
Toast.makeText(mContext, "The app was not allowed to read your contact", Toast.LENGTH_LONG).show();
}
}
}
}
check permissions for marshmallow
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
Manifest
<uses-permission android:name="android.permission.READ_CONTACTS" />
I use RxPermission for permissions to make my code ultimately short.
First add these permissions (or one you need) in your manifest.xml.
<uses-permission android:name="android.permission.READ_CONTACTS" />
Then ask run time permission from user in your activity.
RxPermissions rxPermissions = new RxPermissions(this);
rxPermissions
.request(Manifest.permission.READ_CONTACTS) // ask single or multiple permission once
.subscribe(granted -> {
if (granted) {
// All requested permissions are granted
} else {
// At least one permission is denied
}
});
add this library in your build.gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.tbruyelle:rxpermissions:0.10.1'
implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'
}
Isn't this easy?
As Divyesh Patel pointed out, I had the boolean statemetns mixed up, it should be
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)
Rather than
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED)
Important thing for you to note here that these permissions are asked only for devices with version>23 and if you have lower version of android then for only some models like redmi you have to invoke the permissions manually .
Otherwise version<23 generally do not ask for permissions.
If you put in manifest. It will automatically take it, specially when you are installing app over usb.
If any device has OS version below <23 or In app manifist file maxtarget version is below <23 then it will not ask permission in runtime because while the app installing on these devices you actually giving permission to all you mentioned.
So the runtime permissions are possible only in the case of device has OS version above 22(Lolipop).
Hope this helpful..
#Rajesh
I am developing an SDK(.aar) which third parties can consume. In my sample application when I use the aar, I can see that the application doesnt prompt for the camera permission. When I open the aar and see its manifest.xm, it contains the below:
<!-- WRITE_EXTERNAL_STORAGE is needed because we are storing there the config files of KM -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.read_external_storage" />
<uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.sensor.accelerometer" />
Since the camera is present as a required permission can anyone tell me why it is not coming up when installing the sample app.
Since Android 6.0 you have to request the permssions at runtime and the user is not prompted at the time of installation.
https://developer.android.com/training/permissions/requesting.html
Here is how i check for permissions:
public boolean permissionsGranted() {
boolean allPermissionsGranted = true;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
boolean hasWriteExternalPermission = (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
boolean hasReadExternalPermission = (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
boolean hasCameraPermission = (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED);
if (!hasWriteExternalPermission || !hasReadExternalPermission || !hasCameraPermission) {
allPermissionsGranted = false;
}
}
return allPermissionsGranted;
}
This is how i make the request if permissionsGranted() method returns false
String[] permissions = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.CAMERA, android.Manifest.permission.READ_EXTERNAL_STORAGE};
ActivityCompat.requestPermissions(this, permissions, 1);
And you have to override onRequestPermissionsResult method, below is what i have in mine.
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1: {
boolean startActivity = true;
for (int i = 0; i < grantResults.length; i++) {
if (grantResults.length > 0 && grantResults[i] != PackageManager.PERMISSION_GRANTED) {
String permission = permissions[i];
boolean showRationale = shouldShowRequestPermissionRationale(permission);
if (!showRationale) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getActivity().getPackageName(), null);
intent.setData(uri);
startActivityWithIntent(intent);
Toast.makeText(this, "Enable required permissions", Toast.LENGTH_LONG).show();
startActivity = false;
break;
} else {
Toast.makeText(this, "Enable required permissions", Toast.LENGTH_LONG).show();
break;
}
}
}
if(startActivity) {
getActivity().finish();
startActivityWithIntent(getIntent());
}
}
}
}
public void startActivityWithIntent(Intent intent){
startActivity(intent);
}
showRationale is to check if user ticked never ask again, if so then i start the settings activity so that user can enable permissions.
Let me know if you need more clarity