fix accessing storage on android 6

This commit is contained in:
Milos Kozak 2016-07-04 10:05:21 +02:00
parent b9798fe9f2
commit 9bd7363dbf
2 changed files with 25 additions and 1 deletions

View file

@ -147,10 +147,12 @@ public class MainActivity extends AppCompatActivity {
break;
}
case R.id.nav_export: {
ImportExportPrefs.verifyStoragePermissions(this);
ImportExportPrefs.exportSharedPreferences(this);
break;
}
case R.id.nav_import: {
ImportExportPrefs.verifyStoragePermissions(this);
ImportExportPrefs.importSharedPreferences(this);
break;
}

View file

@ -1,12 +1,15 @@
package info.nightscout.utils;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;
import com.crashlytics.android.Crashlytics;
@ -31,8 +34,27 @@ public class ImportExportPrefs {
static File path = new File(Environment.getExternalStorageDirectory().toString());
static final File file = new File(path, MainApp.resources.getString(R.string.app_name) + "Preferences");
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
//exports shared Preferences
public static void exportSharedPreferences(final Context c) {
new AlertDialog.Builder(c)