I am trying to learn how to use the camera in an app and this is what I reached , the idea is having a button that opens the camera and that the picture will instantly show on the screen after we take the picture, the second button is from an earlier version that couldn't show the picture immediately and had to be clicked in order to show it.
Anyways my problem is that this code doesn't show the picture at android 6.. on my android 5 device it works fine.. the picture is saved in the "sdcard/camera_app/cam_image.jpg" path either way and the button doesn't work as well so I'm thinking something about the imageview has changed from android 5 to 6? the question is pretty much how to make this work for android 6 phones
public class Add_Comment_Picture extends AppCompatActivity {
static final int CAM_REQUEST = 1;
ImageView imageView;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__comment__picture);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.imageView);
Button button2 = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_intent , CAM_REQUEST);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String path = "sdcard/camera_app/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
}
});
}
private File getFile()
{
File folder = new File("sdcard/camera_app");
if (!folder.exists())
{
folder.mkdir();
}
File image_file = new File(folder,"cam_image.jpg");
return image_file;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "sdcard/camera_app/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
super.onActivityResult(requestCode, resultCode, data);
}
}
You need to give READ_EXTERNAL_STORAGE and WRITE_EXTERNAL STORANGE permissions programmatically.
MANIFEST PERMISSIONS WON'T WORK on Android 6
With marshmallow(newest version of Android). We have got some restrictions in Using Sensitive permissions like : Storage,Contacts access, etc..In edition to give these permissions in manifest, We need to request them from users at Runtime.
For more details refer this : Android M permissions
For coding reference please refer this SO question : Android marshmallow request permission?
Add this code in your activity :
#Override
protected void onStart() {
super.onStart();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int hasWritePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
int hasReadPermission = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
int hasCameraPermission = checkSelfPermission(Manifest.permission.CAMERA);
List<String> permissions = new ArrayList<String>();
if (hasWritePermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (hasReadPermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (hasCameraPermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.CAMERA);
}
if (!permissions.isEmpty()) {
requestPermissions(permissions.toArray(new String[permissions.size()]), 111);
}
}
}
Add this after onActivityResult :
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 111: {
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
System.out.println("Permissions --> " + "Permission Granted: " + permissions[i]);
} else if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
System.out.println("Permissions --> " + "Permission Denied: " + permissions[i]);
}
}
}
break;
default: {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
After android 6.0 permission structure has changed. You must check permission on run-time. For example you will select a picture from image gallery, User give permission for gallery access before entering gallery.
You can look this document for this newness.
https://developer.android.com/training/permissions/requesting.html
Sample code for your issue
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
}
If not, you need to ask the user to grant your app a permission:
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
Good luck :)
Related
Issue:
The code is for choosing a file from external storage for doing OTA
update from Android App.This works well in Kitkat but when it comes to
Nougat phones(Moto G4) while choosing the Path App is crashing.
I tried debugging, the issue is with this line file = new File(item.getFilePath()); this creates NullPointerException in Nougat version,I also tried giving permission in Manifest for Reading External Storage,but still issue persists.what could be the issue? I have posted the code below.Please help
public class ChooseImageFileDialog extends Activity {
private FileListAdapter mAdaptor;
private ArrayList<FileFolderItem> mFileFolderList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mChooseButton = (Button) findViewById(R.id.select_button);
mChooseButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mSelectedFile != null) {
getIntent().putExtra(RETURN_PATH_RESULT,
mSelectedFile.getPath());
setResult(RESULT_OK, getIntent());
finish();
}
}
});
mFileFolderList = new ArrayList<FileFolderItem>();
mAdaptor = new FileListAdapter(this, mFileFolderList);
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listview, View view, int
position, long id) {
FileFolderItem item = mFileFolderList.get(position);
if (item == null)
return;
File file = new File(item.getFilePath());
highlightSelectedItem(listview, view);
}
});
Modification :27/11/2017
mChooseButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mSelectedFile != null) {
if (Build.VERSION.SDK_INT >= 23) {
if (checkPermission()) {
Log.e("value", "Permission already Granted, Now you
can save image.");
}
else {
requestPermission();
}
}else {
Log.e("value", "Not required for requesting runtime
permission");
getIntent().putExtra(RETURN_PATH_RESULT,
mSelectedFile.getPath());
setResult(RESULT_OK, getIntent());
finish();
}
}
}
});
private boolean checkPermission() {
int result =
ActivityCompat.checkSelfPermission(ChooseImageFileDialog.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
}else{
return false;
}
}
private void requestPermission()
{
if(ActivityCompat.
shouldShowRequestPermissionRationale(ChooseImageFileDialog.th
is,Manifest.permission.WRITE_EXTERNAL_STORAGE))
{
Toast.makeText(ChooseImageFileDialog.this, "Write External Storage
permission allows us to do store images. Please allow this
permission in App Setting", Toast.LENGTH_LONG).show();
}
else
{
ActivityCompat.requestPermissions(ChooseImageFileDialog.this, new
String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
}
}
I have modified the code to get runtime permissions also but still
Application is crashing.Still having the same problem
You must request permission in Runtime for devices above Android 5.1.
So you need to ask for WRITE_EXTERNAL_STORAGE permission :)
Read the following documentation:
https://developer.android.com/training/permissions/requesting.html
I check for the required permissions on my Log In screen. The dialog shows asking for the 4 permissions I need. All good, but when the dialog appears, the background becomes black and my app closes (doesn't crash, just closes). When I'm done with choosing the permissions I open the app again and it continues running from where it stopped. How can I make the app continue running while the permissions dialog is shown? I use a class which checks for permissions and call it in the Log In activity.
The class:
public abstract class RuntimePermissionsActivity extends AppCompatActivity {
private SparseIntArray mErrorString;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mErrorString = new SparseIntArray();
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
int permissionCheck = PackageManager.PERMISSION_GRANTED;
for (int permission : grantResults){
permissionCheck = permissionCheck + permission;
}
if ((grantResults.length>0) && permissionCheck == PackageManager.PERMISSION_GRANTED){
onPermissionsGranted(requestCode);
}
else {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
}
}
public void requestAppPermissions(final String[] requestedPermissions, final int requestCode){
int stringId = 0;
mErrorString.put(requestCode, stringId);
int permissionCheck = PackageManager.PERMISSION_GRANTED;
boolean shouldShowRequestPermissionRationale = false;
for (String permission : requestedPermissions){
permissionCheck = permissionCheck + ContextCompat.checkSelfPermission(this, permission);
shouldShowRequestPermissionRationale = ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
}
if(permissionCheck != PackageManager.PERMISSION_GRANTED){
if(shouldShowRequestPermissionRationale){
ActivityCompat.requestPermissions(RuntimePermissionsActivity.this, requestedPermissions, requestCode);
}
else {
ActivityCompat.requestPermissions(this, requestedPermissions, requestCode);
Toast.makeText(RuntimePermissionsActivity.this, "Please relaunch the application in order for the changes to take effect!", Toast.LENGTH_LONG).show();
}
}
else {
onPermissionsGranted(requestCode);
}
}
public abstract void onPermissionsGranted(int requestCode);
}
The Log In activity call:
LoginActivity.super.requestAppPermissions(new
String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSIONS);
I faced same issue,check if activity android:noHistory="true" in your Manifest file.
Same problem here.
ankit jain was on the track. Removing android:noHistory="true" fixed the N.Park problem.
Although in my case I needed that value to be true, or at least the behaviour that makes (I had a SplashScreen where I managed the permissions, and after moving on, I wanted that Activity out of my stack).
So:
I deleted line android:noHistory="true"
In the activity I manage the permission without problems
After that, I move into the next activity with:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
An example of implementing Permissions in any Activity.
SampleActivity.java
public class SampleActivity extends AppCompatActivity{
private final int PERMISSION_CODE = 1;
Button button;
#override
onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.your_layout);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
#override
public void onClick(View view){
requestPermissionAndContinue();
}
});
//remaining code to continue using the app
//your actual code should also be in this same class
}
private void requestPermissionAndContinue(){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)){
Log.e(TAG, "permission denied, show dialog");
}else{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, PERMISSION_CODE);
}
}else{
accessContacts();
}
}
private void accessContacts(){
//your code once you receive permission
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(grantResults.length > 0 && permissions[0]==PackageManager.PERMISSION_GRANTED){
accessContacts();
}else{
//redirect to settings page or ask permission again
}
}
}
Remove android:noHistory="true" attribute from respective RuntimePermissionsActivity tag from Android manifest file solved that problem.
Write just a few lines of codes:
final int requestCode=100;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {
String[] per = {Manifest.permission.READ_CONTACTS};
requestPermissions(per, requestCode);
if (ActivityCompat.checkSelfPermission(CollectZone.this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
// recall permission until is not permitted
} else {
// write execution code here
}
}
I'm trying to make a basic camera application that can access the saved photo from the gallery (needed as part of another app but due to problems I've been having I am developing this in a blank project), and have been following mainly this tutorial https://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media
Having then realised that it would just crash because of how permissions work in Marshmallow, and needing backward compatibility, I've tried to follow tutorials on implementing permission requests so that I can actually use the app.
This is what I currently have after several hours of trying. I have added permissions in the manifest, but as these are fairly standard I have not bothered to copy and paste these over. This currently crashes on the test() method because of there not being a group called Storage. With that line commented out, it will just say permission denied without prompting me to sort permissions (whether on a Marshmallow device or not). Frankly I am now at a loss. What I need this to do is before launching the camera in the onLaunchCamera method (which is launched off a button click), to get the permissions for reading and writing external storage and for accessing the camera. Any help you are able to give would be much appreciated.
private boolean cameraPermissionsCheck() {
return ContextCompat.checkSelfPermission(this, Manifest.permission_group.CAMERA) == PackageManager.PERMISSION_GRANTED;
}
private boolean storagePermissionsCheck() {
return ContextCompat.checkSelfPermission(this, Manifest.permission_group.STORAGE) == PackageManager.PERMISSION_GRANTED;
}
private void requestPermissions() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission_group.CAMERA, Manifest.permission_group.STORAGE}, 123);
}
private void test() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission_group.STORAGE)) {
//was a toast notification here
requestPermissions();
} else {
requestPermissions();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 123
&& grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
public void onLaunchCamera(View view) {
//btn = (Button) findViewById(R.id.button);
if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
if(!cameraPermissionsCheck() || !storagePermissionsCheck()){
test();
}
else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName)); // set the image file name
if (intent.resolveActivity(getPackageManager()) != null) {
// Start the image capture intent to take photo
startActivityForResult(intent, 0);
}
}
} else {
Toast.makeText(MainActivity.this, "No Camera",
Toast.LENGTH_LONG).show();
}
}
Try this
public void onLaunchCamera(View view) {
//btn = (Button) findViewById(R.id.button);
if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
checkPermission();
}
else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName)); // set the image file name
if (intent.resolveActivity(getPackageManager()) != null) {
// Start the image capture intent to take photo
startActivityForResult(intent, 0);
}
}
} else {
Toast.makeText(MainActivity.this, "No Camera",
Toast.LENGTH_LONG).show();
}
}
private void checkPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {//Can add more as per requirement
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
123);
} else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,getPhotoFileUri(photoFileName)); // set the image file name
if (intent.resolveActivity(getPackageManager()) != null) {
// Start the image capture intent to take photo
startActivityForResult(intent, 0);
}
}
And make sure you have set proper version in your build.gradle
**compileSdkVersion 23
buildToolsVersion "23.0.2"**
defaultConfig {
applicationId "your_package_name"
minSdkVersion 15
**targetSdkVersion 23**
versionCode 1
versionName "1.0"
multiDexEnabled true
}
From my activity when i call Contacts to get a contact number the application crashed. Application is running ok on other devices but when i'm try to run it in android 6.0, it crashed. i've no idea what i'm doing wrong.
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
After getting the the data in onActivityResult method.
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = this.getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String phoneNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
Log.d(TAG, "name : "+name+" , Phone Number : "+ phoneNumber);
}
}
break;
}
}
can Anyone help me ?
All of the answers here are wrong. You shouldn't need READ_CONTACT permission at all to retrieve single contact using ACTION_PICK intent. You should be granted this permission temporary to be able to retrieve contact specific data. However there are some devices that don't implement this API in a good way. Myself I have found Sony Xperia Z3, but I heard about HTC devices also had this problem.
To have the user select a contact and provide your app access to all the contact information, use the ACTION_PICK action and specify the MIME type to Contacts.CONTENT_TYPE.
The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact. The response grants your app temporary permissions to read that contact using the Contacts Provider API even if your app does not include the READ_CONTACTS permission.
Source: https://developer.android.com/guide/components/intents-common.html#Contacts
[UPDATE]
After upgrading Sony Xperia Z3 system image to version 23.5.A.1.291 bug does not occur anymore.
From android 6.0 (Mashmello) , android has introduced Run Time Permissions for users to grand permission to apps while the app is running, not when they install the app. So, Your calling should be like this ->
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
/*If Android M*/
if (ContextCompat.checkSelfPermission(SendMoneyByDetailsActivity.this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
/*If Android M and Not permission granted */
ActivityCompat.requestPermissions(SendMoneyByDetailsActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACTS);
} else {
/*If Android M and permission granted */
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
} else {
/*IF not Android M*/
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
And when user grant the permission then you can call the intent to pick contact list ->
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission was granted, yay! Do the
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
} else {
// permission denied
}
return;
}
}
}
And don't forgot to add permission in your manifest file.
<uses-permission android:name="android.permission.READ_CONTACTS" />
It not your manifest problem its problem with android 6.0 it requires Runtime Permissions...
after adding this in manifest
<uses-permission android:name="android.permission.READ_CONTACTS" />
Just change your code to this In your code..
public class MainActivity extends Activity {
public static final int REQUIRED_PERMISSIONS = 001;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//give here permissions what ever you want...
// if you are using one add only one... not all..
if ((CheckPermission(this, Manifest.permission.READ_CONTACTS))&&
(CheckPermission(this, Manifest.permission.READ_PHONE_STATE))&&
(CheckPermission(this, Manifest.permission.INTERNET))&&
(CheckPermission(this, Manifest.permission.ACCESS_NETWORK_STATE))&&
(CheckPermission(this, Manifest.permission.ACCESS_WIFI_STATE)))
{
PermHandling();
}
//now reqest runtime permissions..
else {
RequestPermission(MainActivity.this, Manifest.permission.READ_CONTACTS, REQUIRED_PERMISSIONS);
RequestPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE, REQUIRED_PERMISSIONS );
RequestPermission(MainActivity.this, Manifest.permission.INTERNET, REQUIRED_PERMISSIONS );
RequestPermission(MainActivity.this, Manifest.permission.ACCESS_NETWORK_STATE, REQUIRED_PERMISSIONS );
RequestPermission(MainActivity.this, Manifest.permission.ACCESS_WIFI_STATE, REQUIRED_PERMISSIONS );
}
}
private void PermHandling() {
//Your app internal parts....
//Here your stuff works...
}
//private void NewPermHandling(){
//}
#Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
switch (permsRequestCode) {
case REQUIRED_PERMISSIONS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
PermHandling();
} else {
//Toast.makeText(this, "Please Grant Permissions other wise app will close.!", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
public void RequestPermission(Activity thisActivity, String Permission, int Code) {
if (ContextCompat.checkSelfPermission(thisActivity,
Permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Permission)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Permission},
Code);
}
}
}
public boolean CheckPermission(Context context, String Permission) {
if (ContextCompat.checkSelfPermission(context,
Permission) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
}
I also faced same problem my applications worked on till 5.1 but 6.0+ due to Runtime permissions We need to add them.. and accept those permissions at runtime
I think this is permission issue
add bellow permission inside manifest file
<uses-permission android:name="android.permission.READ_CONTACTS" />
accept permissions in marshmallows
https://developer.android.com/training/permissions/requesting.html
actual implementation
add bellow code inside Main Activity
call checkAndAddPermission() method inside onCreate() method
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 1244;
private void checkAndAddPermission() {
List<String> permissionsNeeded = new ArrayList<>();
final List<String> permissionsList = new ArrayList<>();
if (!addPermission(permissionsList, android.Manifest.permission.READ_CONTACTS))
permissionsNeeded.add("ReadContacts");
if (permissionsList.size() > 0) {
if (permissionsNeeded.size() > 0) {
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
ActivityCompat.requestPermissions(MainActivity.this, permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
} else {
ActivityCompat.requestPermissions(MainActivity.this, permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
}
}
#TargetApi(Build.VERSION_CODES.M)
private boolean addPermission(List<String> permissionsList, String permission) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
return false;
}
return true;
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<>();
// Initial
perms.put(android.Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(android.Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
I play around with the Preview M and test my app on it, especially the "saving a file to external storage" part.
Before the download/save process starts, i request for
Manifest.permission.WRITE_EXTERNAL_STORAGE
permission as described on the developer page: https://developer.android.com/preview/features/runtime-permissions.html
The dialog occurs as expected:
"Allow <AppName> to access photos, media, and files on your device?" Deny / Allow
If i hit the "deny"-button, the onRequestPermissionsResult-method of the activity is called.
If i press the "allow"-button, the activity is first recreated and afterwards the onRequestPermissionsResult-method is called. I think it's a result of the granted permission.
But the second situation is tricky because i want to trigger a callback that starts the download, but this object is null at this point:
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
getLogger().error("onRequestPermissionsResult ( " + requestCode + ", " + permissions + ", " + grantResults + " )");
switch (requestCode) {
//permission for saving files?
case PermissionCode.WRITE_EXTERNAL_STORAGE: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//HERE IS THE NULL-OBJECT
if (controller != null) {
controller.triggerCallback();
}
}
break;
}
default: {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
So my questions are:
Can I avoid this recreation?
If no, how can i redesign my code to solve the problem - i'm totally idealess at the moment
EDIT 1:
I tried to solve the problem with a handler and postDelayed - but i skipped it.
EDIT 2:
I preliminary solved it and show a Toast to user with the request to push the download button again (in case of granted permission). But Google: ARE YOU SERIOUS?
EDIT 3:
No recreation happens with the latest SDK 6.0 (Version 23) - maybe somebody heard my weeping in Mountain View :-)
You can always check the download condition immediately after the activity recreates itself in onCreate():
static final int MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
boolean initiateDownload = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
initiateDownload = savedInstanceState.getBoolean("toDownload");
}
}
#Override
public void onResume() {
super.onResume();
final boolean hasPermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED;
if(initiateDownload && hasPermission) {
// start download here...
} else {
requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE},
MainActivity.MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE) {
if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initiateDownload = true;
} else {
// denied permission...
}
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("toDownload", initiateDownload);
}
Android may destroy and recreate your activity for a number of reasons. The use case of requesting permissions is no exception. You can use your preferred way of saving and restoring UI state. If your state is parcelable you can do it like the example below. Why this is not mentioned in Request App Permissions or Android RuntimePermissions Sample I don't know.
See more in
Saving and restoring transient UI state
and
Saving UI States
public class ExampleActivity extends AppCompatActivity {
private static final String CURRENT_DOWNLOAD_ITEM = "CURRENT_DOWNLOAD_ITEM";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
currentDownloadItem = savedInstanceState.getParcelable(CURRENT_DOWNLOAD_ITEM);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putParcelable(CURRENT_DOWNLOAD_ITEM, currentDownloadItem);
super.onSaveInstanceState(outState);
}
public void downloadItem(Parcelable item) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
currentDownloadItem = item;
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else
perfornDownload(item);
}
private Parcelable currentDownloadItem;
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (currentDownloadItem != null) {
perfornDownload(currentDownloadItem);
currentDownloadItem = null;
} else
Toast.makeText(this, "Please select download again now that we have permission to save it", Toast.LENGTH_LONG).show();
} else
Toast.makeText(this, "Download cancelled, as we don\'t have permission to save the downloaded files", Toast.LENGTH_LONG).show();
break;
}
}
private void perfornDownload(Parcelable item) {
// Here we are sure we have the permission
}
}