Reputation: 23
I have created a simple application which tries to access a phone's external storage files and display it on an alert box the code goes something like this.
btnShowDeviceFolders.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CharSequence items[];
final List<String> list = new ArrayList<String>();
final File path =
Environment.getExternalStoragePublicDirectory
(
//Environment.DIRECTORY_PICTURES
Environment.DIRECTORY_DCIM
//File.separator +"/"
);
if(!path.exists())
{
//path.mkdirs();
System.out.println("No file found");
}
String[] files = path.list();
if (files.length == 0) {
//System.out.println("The directory is empty");
} else {
for (String aFile : files) {
//System.out.println(aFile);
list.add(aFile);
}
}
items = files;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Select Project Folder");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(),list.get(item),Toast.LENGTH_LONG).show();
}
});
builder.show();
}
});
But this code works fine on the emulator it lists me the folders that are present, but when i build the apk
and test it on my mobile it crashes(I'm using android Oreo for testing). The Stacktrace does not give any errors
What can I do to overcome this issue.
Upvotes: 0
Views: 221
Reputation: 185
Give permission for external memory in the android_manifest.xml file.
Upvotes: 1
Reputation: 136
You are only giving permission in manifest that is not sufficient you need to give runtime permission .
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//Show Information about why you need the permission
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Need Storage Permission");
builder.setMessage("This app needs storage permission.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else if (permissionStatus.getBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE,false)) {
//Previously Permission Request was cancelled with 'Dont Ask Again',
// Redirect to Settings after showing Information about why you need the permission
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Need Storage Permission");
builder.setMessage("This app needs storage permission.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
sentToSettings = true;
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
Toast.makeText(getBaseContext(), "Go to Permissions to Grant Storage", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else {
//just request the permission
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
}
SharedPreferences.Editor editor = permissionStatus.edit();
editor.putBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE,true);
editor.commit();
} else {
//You already have the permission, just go ahead.
proceedAfterPermission();
}
Upvotes: 0
Reputation: 180
You have to give android.permission.READ_EXTERNAL_STORAGE
run time.
here is the tutorial
Upvotes: 0