0

I created an app and its supposed to ask for permissions at run time . But its not asking for any permissions. I am able to grant permissions manually from settings but its not requesting permissions on its own. Also when click on button,its supposed to ask for permissions and after permissions being granted , its supposed to display images . No images are being displayed even after I grant permissions manually from settings. How can I fix that ? Can anyone help ?

Bacon1 is the activity which contains the button getting clicked , and on Click its supposed to switch to ActivityHome

Bacon1.java:

public class Bacon1 extends Activity {

    private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
    private static int RESULT_LOAD_IMAGE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bacon1);


    }

    private void buttonClicked(View view) {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE) + ContextCompat
                .checkSelfPermission(this,
                        Manifest.permission.INTERNET)
                != PackageManager.PERMISSION_GRANTED) {

            Snackbar.make(view, "Permission not Granted, Requesting permission.", Snackbar.LENGTH_LONG).show();
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {

                Snackbar.make(view, "We need permission to internal storage for displaying songs", Snackbar.LENGTH_LONG).show();

            } else {

                Snackbar.make(view, "Allow myapp3 to access this device's internal storage", Snackbar.LENGTH_LONG).show();

                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
    }


    public void onClick2(View view) {

        buttonClicked(view);
        Intent viewIntent2 = new Intent(this, ActivityHome.class);
        startActivity(viewIntent2);
    }




    public void onClick5(View view) {

        Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.getExternalStorageDirectory() + "/myapp") + "/" + "downloadedfile.zip");
        viewIntent1.setDataAndType(Uri.fromFile(file), "application/zip");
        startActivity(Intent.createChooser(viewIntent1, null));
    }





    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(Bacon1.this, "WRITE_CONTACTS granted", Toast.LENGTH_SHORT)
                            .show();


                } else {

                    Toast.makeText(Bacon1.this, "WRITE_CONTACTS Denied", Toast.LENGTH_SHORT)
                            .show();

                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
}

ActivityHome.java:

public class ActivityHome extends AppCompatActivity implements SlideMenuAdapterInterface{

    private Context mContext;
    private Toolbar toolbar;
    private DrawerLayout Drawer;
    private ActionBarDrawerToggle mDrawerToggle;
    private FragmentManager fragmentManager = null;
    private FragmentTransaction fragmentTransaction = null;
    private Fragment currentFragment=null;

    private ListView slidingList;
    private SlideMenuAdapter mSlideMenuAdapter;
    private int currentPosition=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

         mContext=getApplicationContext();
         initializeActionBar();
         initialCalling();

    }

    @Override
    public void onBackPressed() {
        if(Drawer.isDrawerOpen(Gravity.LEFT)){
            Drawer.closeDrawer(Gravity.LEFT);
        }else{
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void slideRowClickEvent(int postion) {
        if (currentPosition== postion) {
            closeDrware();
            return;
        }
        currentPosition= postion;
        getFragment(postion);
        attachedFragment();
    }

    private void initializeActionBar() {
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);

        slidingList=(ListView)findViewById(R.id.sliding_listView);
        mSlideMenuAdapter=new SlideMenuAdapter(mContext, getSlideList());
        mSlideMenuAdapter.setSlidemenuadapterinterface(this);
        slidingList.setAdapter(mSlideMenuAdapter);

        Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);
        mDrawerToggle = new ActionBarDrawerToggle(this, Drawer, toolbar,
                R.string.openDrawer, R.string.closeDrawer) {

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }

        };
        Drawer.setDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();


    }

    private void closeDrware(){
        if(Drawer.isDrawerOpen(Gravity.LEFT)){
            Drawer.closeDrawer(Gravity.LEFT);
        }
    }

    private void initialCalling(){
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();

        getFragment(0);
        attachedFragment();
    }



    private void attachedFragment(){
        try {
            if (currentFragment != null) {
                if (fragmentTransaction.isEmpty()) {
                    fragmentTransaction.add(R.id.fragment_container, currentFragment,"" + currentFragment.toString());
                    fragmentTransaction.commit();
                    toolbar.setTitle(title[currentPosition]);
                }else {
                    fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.fragment_container, currentFragment,"" + currentFragment.toString());
                    fragmentTransaction.commit();
                    toolbar.setTitle(title[currentPosition]);
                }

            }
            closeDrware();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void getFragment(int postion){
        switch (postion) { 
        case 0:
            currentFragment = new GalleryFragment();
            break;
        case 1:
            currentFragment = new CameraFragment();
            break;
        case 2:
            currentFragment = new VideoFragment();
            break;

        default:
            break;
        }
    }


    /**
     * Slide Menu List Array.
     */
    private String[] title={"All Images","Camera","Video"};
    private int[] titleLogo={R.drawable.selector_allpic,R.drawable.selector_camera,R.drawable.selector_video};
    private ArrayList<SlideData> getSlideList(){
        ArrayList<SlideData> arrayList=new ArrayList<SlideData>();
        for (int i = 0; i < title.length; i++) {
            SlideData mSlideData=new SlideData();
            mSlideData.setIcon(titleLogo[i]);
            mSlideData.setName(title[i]);
            mSlideData.setState((i==0)?1:0);
            arrayList.add(mSlideData);
        }
        return arrayList;
    }
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dell_1.copycat2">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
    <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
    <uses-permission android:name="android.permission.INTERNET" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".apples"
            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>
        <activity
            android:name=".Bacon1"
            android:label="activity_bacon1" />
        <activity
            android:name=".activity.ActivityHome"
            android:label="activity_home" />
        <activity
            android:name=".activity.AlbumActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".activity.PhotoPreviewActivity"
            android:label="@string/app_name">
        </activity>
    </application>

</manifest>

build.gradle :

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.example.dell_1.copycat2"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:26.+'
    compile files('libs/universal-image-loader-1.9.2-SNAPSHOT-with-sources.jar')
    testCompile 'junit:junit:4.12'
}
  • If I replace + by && , it gives error : Operator '&&' cannot be applied to 'int', 'boolean' @Pavneet_Singh –  Sep 23 '17 at 16:14
  • Did you resolve this ? I have the same problem, it works on 5.1 but does not seem to work ob versions above that. – Mark Anderson Feb 20 '21 at 11:07

1 Answers1

0

First INTERNET is not a dangerous permission and therefore has no role with runtime permissions. Second, you cannot add two checkSelfPermission() results together and expect something useful.

Remove + ContextCompat .checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED

and see if that clears up your problem.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491