Android Photo/Media/File Runtime permission problem - java

I need Photo, Media & File permission to keep my app data in CSV format in my local phone storage.
public void permissionTake() {
if (ContextCompat.checkSelfPermission(Activity_login.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(Activity_login.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(Activity_login.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
} else {
}
}
Those line of codes works fine in a previous project with min android
version 16 and targeted version 27. But in a new project it only ask
for 'photos and media' permission. Not 'Files'. For that the CSV file
is failed to stored in my storage.

Related

is using IMEI as unique ID and storing it in database against Google play privacy?

I have got an app with sign up page (firebase authentication) and I want to allow user to make only one account per device
What I've Tried
using the method below I have tried getting IMEI as unique UID and I stored it in my database so I can check if the user already made an account on my app using their device
private String GetDeviceID(){
TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String deviceID = null;
int readIMEI= ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE);
if(deviceID == null) {
if (readIMEI == PackageManager.PERMISSION_GRANTED) {
deviceID = tm.getDeviceId();
}
}
return deviceID;
}
Manifest permission
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"/>
My question is
since I'm going to publish my app to Play store
is it against Google play privacy to get user IMEI and store it in database?
I have read their policy but I'm still not sure
or is there any other unique device ID or any method that I can use to achieve this
use The Firebase installations service (FIS) which provides a Firebase installation ID (FID) for each installed instance of a Firebase app.
in your app gradle:
implementation 'com.google.firebase:firebase-installations:17.0.0'
to Retrieve client identifiers, in you main activity:
FirebaseInstallations.getInstance().getId()
.addOnCompleteListener(new OnCompleteListener<String>() {
#Override
public void onComplete(#NonNull Task<String> task) {
if (task.isSuccessful()) {
Log.d("Installations", "Installation ID: " + task.getResult());
} else {
Log.e("Installations", "Unable to get Installation ID");
}
}
});

CAPTURE_AUDIO_OUTPUT not asked for permission at runtime

im trying to record sound from dictaphone but on android 6+ i get permission error.
I add code for asking permission (have 3 permissions for ask ) 2 work but
CAPTURE_AUDIO_OUTPUT show error. Its just not ask me to grant permission. In logs its just "not granted"
Any one know in what problem ?
public static boolean PermissionCheck(Activity context, String permission, int code) {
boolean state = false;
int permissionCheck = ContextCompat.checkSelfPermission(context,
permission);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(context, new String[]{permission}, code); // define this constant yourself
} else {
// you have the permission
return true;
}
return state;
}
case CAPTURE_AUDIO_OUTPUT_CONSTANT: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Logger.e("CAPTURE PERMISSION GRANTED");
INIT();
} else {
Logger.e("CAPTURE PERMISSION NOT GRANTED");
finish();
}
return;
}
error
W/PackageManager: Not granting permission android.permission.CAPTURE_AUDIO_OUTPUT to package blabla_package (protectionLevel=18 flags=0x3848be46)
in manifest
<uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT"/>
[UPD] After a lot of tries and researches i now can answer :
Thank to google , now we cant record calls.
Its possible only if using C code and NDK .
CAPTURE_AUDIO_OUTPUT is not a dangerous permission and so does not work with the runtime permission system. CAPTURE_AUDIO_OUTPUT has android:protectionLevel="signature|privileged", so it can only be held by apps that are installed on the privileged (a.k.a., system) partition or are signed by the platform signing key.

Android Version 4.3 above listFiles() always returns Null Why?

I have created a File manager App which Lists Asynchronously files from the SDCard into a Recycler View.
private class AsyncFileListLoader extends AsyncTask<String, Void, Boolean> {
protected void onPreExecute() {
//Shows the Custom Progress Dialog
}
#Override
protected Boolean doInBackground(String... param) {
File oneFile=new File(mother_folder);
/*This code causes the App to Crash by Null pointer exception.
Happens only in Android ver 4.3 and above.WHY??*/
File[] files = oneFile.listFiles();
if(files.length ==0) return false;
/* THIS IS ALWAYS FALSE ONLY IN VERSION 4.3 and above WHY??*/
}
///Rest of the code///
}
}
Executed thus
String DirPath=Environment.getExternalStorageDirectory().getAbsolutePath();
/* Correctly displays the path to External storage==Sdcard or Emulated in ver 4.4 and above*/
Toast.makeText(this,DirPath, Toast.LengthShort).show();
/* causes app to crash
new AsyncFileListLoader().execute(DirPath);
I have setup correct permissions in Android manifest and List is populated in
Android ver 4.2 Phone. I check the run time permission for SD card thus
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and
Use of RUN time permissions as per this thread
listFiles() returns null on Android 6.0 emulator
int permissionCheck1 = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
int permissionCheck2 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck1 != PackageManager.PERMISSION_GRANTED || permissionCheck2 != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_READWRITE_STORAGE);
}
Check self permission returns true. I DO HAVE permission to READ and Write External storage. After 1 week of searching I have no solution. This
File[] files = oneFile.listFiles();
Returns null always, plus Using FileUtils.listFiles DOES NOT RETURN null why? Internally,I checked source code here,FileUtils uses the same ListFile...
https://commons.apache.org/proper/commons-io/javadocs/api-2.5/src-html/org/apache/commons/io/FileUtils.html
There seems to be No other function which can replace listfiles()..
Thanks a heap in advance.

Android targetSdkVersion 23 checkSelfPermission method

I am facing checking permission value in Android 6.0 (API 23). Always get 0 value even permission enable or disable from app's settings.
Below is step which I taken.
Contact permission manually disable from device settings->apps-> My apps-> permission -> Disable contact permission.
Still In Android 6.0 every time got 0 value when execute below line of code.
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)
Below is my code. which I define in main launcher activity class
// Identifier for the permission request
private static final int WRITE_CONTACTS_PERMISSIONS_REQUEST = 9;
........
#Override
public void onCreate(Bundle savedInstanceState) {
....
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.LOLLIPOP_MR1)
{
sharedPreferencesEditor.putBoolean(getString(R.string.ALLOW_ACCESS_PHONEBOOK), true);
sharedPreferencesEditor.commit();
}
else {
getPermissionToReadUserContacts();
}
....
}
// Called when the user is performing an action which requires the app to read the
// user's contacts
public void getPermissionToReadUserContacts() {
// 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid
// checking the build version since Context.checkSelfPermission(...) is only available
// in Marshmallow
// 2) Always check for permission (even if permission has already been granted)
// since the user can revoke permissions at any time through Settings
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// The permission is NOT already granted.
// Check if the user has been asked about this permission already and denied
// it. If so, we want to give more explanation about why the permission is needed.
if (shouldShowRequestPermissionRationale(
Manifest.permission.WRITE_CONTACTS)) {
// Show our own UI to explain to the user why we need to read the contacts
// before actually requesting the permission and showing the default UI
}
// Fire off an async request to actually get the permission
// This will show the standard permission request dialog UI
requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS},
WRITE_CONTACTS_PERMISSIONS_REQUEST);
}
}
// Callback with the request from calling requestPermissions(...)
#Override
public void onRequestPermissionsResult(int requestCode,
#NonNull String permissions[],
#NonNull int[] grantResults) {
// Make sure it's our original READ_CONTACTS request
if (requestCode == WRITE_CONTACTS_PERMISSIONS_REQUEST) {
if (grantResults.length == 1 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Write Contacts permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Write Contacts permission denied", Toast.LENGTH_SHORT).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
If you target SDK 23 (Android 6), all of the permissions (in your manifest) are disabled by default, whereas if your targetSDK is 22 (Android 5.1) and your app is running on Android 6, all of the permissions are enabled by default when the user installs the app, and even if the user revokes the permissions later on, checkSelfPermission returns incorrect value of PERMISSION_GRANTED
It is also available in the documentation of PermissionChecker
In the new permission model permissions with protection level dangerous are runtime permissions. For apps targeting M and above the user may not grant such permissions or revoke them at any time. For apps targeting API lower than M these permissions are always granted as such apps do not expect permission revocations and would crash. Therefore, when the user disables a permission for a legacy app in the UI the platform disables the APIs guarded by this permission making them a no-op which is doing nothing or returning an empty result or default error.
I had the same issue but I realized that there were locals modules with target version below 23. After removing them, the bug was resolved.

An error occurred while connecting to camera: 0 --- Fail to connect to camera service

I have a phone specific problem when I open a camera on this one particular Nexus 5. Its the An error occurred while connecting to camera: 0 --- Fail to connect to camera service error. On at least a dozen other phones everything works just fine. Other apps that use the camera on the Nexus 5 are not crashing (indicating its not all apps that can't access the camera, just mine).
There are a number of other questions on this and I have tried all of them. They all talk about missing permissions, and making sure the camera is destroyed after use.
To be clear my manifest requests and uses the camera properly:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera2" />
And I am releasing the camera when destroying:
public void releaseCamera()
{
if (_camera != null)
{
_camera.stopPreview();
_camera.setPreviewCallback(null);
_camera.release();
_camera = null;
}
_surfaceHolder.removeCallback(this);
}
Can you think of any reason what-so-ever that I am getting this. I have a suspicion that theres some sort of bug because I am using camera and not camera2 but that is a wild guess. Reading the updates for API 6.0 there is a section on camera that says:
In This release, the model for accessing shared resources in the
camera service has been changed from the previous “first come, first
serve” access model to an access model where high-priority processes
are favored.
Again without re-writing the entire app to use camera2 (not an option) I can't say for certain what's going on.
Here is my code where I open the camera (and what works on every other phone except the Nexus 5)
private void setInitialCamera()
{
try
{
if (_isBackFacing == true)
{
_camera = Camera.open(0);
} else
{
_camera = Camera.open(1);
}
} catch (RuntimeException e)
{
Log.d("Runtime Exception","Error " + e);
e.printStackTrace();
} catch (Exception e)
{
Log.d("Camera Error: ", " Android is unable tell what the error was");
e.printStackTrace();
}
}
So it looks like the culprit has something to do with the 6.0.1 update this phone went through.
While this didn't happen on other phones, it did on the failing Nexus 5.
What happened was the 6.0.1 update allows users to set individual permissions for an app. So somehow the persmission for the camera were toggled off. Turning this back on fixed the issue.
To get there you go to Settings -> Apps -> [App Name] -> Permissions
Making user to set permissions manually for your app is not a good approach. Use following code instead, which wil prompt user permission when your app is launched for first time.
First set your request code, which is used to recognize accepted or refused request:
private static final int MY_CAMERA_REQUEST_CODE = 100;
Then ask the user if you can use the camera:
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_REQUEST_CODE);
}
else {
// permission has been already granted, you can use camera straight away
}
Finally check whether permissions were granted:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// user accepted your request, you can use camera now from here
}
else {
// user denied your request, you can now handle their decision
}
}
}

Categories

Resources