nah1991 nah1991 - 10 months ago 43
Android Question

Android new storage permission for download

Recently i work to develope an Android which do search and download files... The app works fine in old versions of Android and but it was crashing in version 6 and on... so i add a new permission for file storage.
Now when i open the app and want to search something , it gives me a notification about accessing media and storage and asks me to allow or deny..i accept it but after search it asks again and when i press download button, it ask again ...
Would it be possible if the app asks only once or maybe when a new user install it for the first time, it asks them and then not again and again?

This is part of the code which i think maybe helpful for you to see me improve this.

// Declaring Your View and Variables

Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence titles[] = {"Search Results"};
CharSequence titles2[] = {""};
int numbOfTabs = titles.length;
int numbOfTabs2 = titles2.length;
private AdView mAdView;

protected void onCreate(Bundle savedInstanceState) {

new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);

// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(;

Intent intent = getIntent();

if (Intent.ACTION_SEARCH.equals(intent.getAction()))
adapter = new ViewPagerAdapter(getSupportFragmentManager(), titles, numbOfTabs, false);
adapter = new ViewPagerAdapter(getSupportFragmentManager(), titles2, numbOfTabs2, true);

// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(;

// Assigning the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(;
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabs_scroll_color);


// Setting the ViewPager For the SlidingTabsLayout

if (Intent.ACTION_SEARCH.equals(intent.getAction())) {

assert getSupportActionBar() != null;

String query = intent.getStringExtra(SearchManager.QUERY);


public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(, menu);

// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(;

return true;


Answer Source

You should just check if the permission as already been granted before asking for it:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);