check for external storage permission at startup

This commit is contained in:
tibbi 2016-02-26 12:19:38 +01:00
parent 1045b53f9a
commit bc2f333171
2 changed files with 29 additions and 1 deletions

View file

@ -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<Directory> 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);
}

View file

@ -1,4 +1,5 @@
<resources>
<string name="app_name">Simple Gallery</string>
<string name="share_via">Share via</string>
<string name="no_permissions">Not much to do in a gallery without accessing your photos</string>
</resources>