From bc2f333171a1f59c737adc293215f9e2bbe0ae58 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 26 Feb 2016 12:19:38 +0100 Subject: [PATCH] check for external storage permission at startup --- .../gallery/activities/MainActivity.java | 29 ++++++++++++++++++- app/src/main/res/values/strings.xml | 1 + 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java index 563a0bc32..43700b0bd 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java @@ -1,15 +1,20 @@ package com.simplemobiletools.gallery.activities; +import android.Manifest; import android.content.Intent; +import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; +import android.widget.Toast; import com.simplemobiletools.gallery.Constants; import com.simplemobiletools.gallery.Directory; @@ -24,16 +29,38 @@ import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity implements OnItemClickListener { + private final int STORAGE_PERMISSION = 1; private List dirs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - final GridView gridView = (GridView) findViewById(R.id.directories_grid); + if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { + initializeGallery(); + } else { + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION); + } + } + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + + if (requestCode == STORAGE_PERMISSION) { + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + initializeGallery(); + } else { + Toast.makeText(this, getResources().getString(R.string.no_permissions), Toast.LENGTH_SHORT).show(); + } + } + } + + private void initializeGallery() { dirs = new ArrayList<>(getDirectories().values()); final DirectoryAdapter adapter = new DirectoryAdapter(this, dirs); + + final GridView gridView = (GridView) findViewById(R.id.directories_grid); gridView.setAdapter(adapter); gridView.setOnItemClickListener(this); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 834e2a1e4..eb7526ece 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,4 +1,5 @@ Simple Gallery Share via + Not much to do in a gallery without accessing your photos