+++ date = “2016-01-30T00:13:56Z” title = “Project 2: File Explorer for android”
+++
In this project I am building a file explorer library for android. As I was working on catcher it became obvious I would need a file picker and explorer solution. So I did a bit of looking on the web. I found three interesting projects that nearly did what I wanted. I put a few of them together to come up with a hybrid solution.
So I spent the last couple of day merging these projects into a single hybrid. It is going pretty well but not finished yet. You can find the current version of this project here: File Explorer
This will be an android library project. It is not the first time I have needed a file explorer for android so I am sure I will reuse it going forward.
I was a bit surprised here, I looked and tried a few things but really nothing worked. In the end I just copied another library project template and renamed the paths.
Make sure and include the app and library in you base settings.gradle
include ':app', ':library'
Intellij actually prompted me to do this which I thought was pretty cool. To add it just add it as a compile option
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile project(':library')
}
I found that when I transferred the apk to the emulator my permissions were not automatically applied. This was rather inconvenient. So i had to manually apply them in the emulator using the system tool
Here is some code (Android M4 +) to fix that:
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
ActivityCompat.requestPermissions(FileExplorerActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_FOR_READ_EXTERNAL_STORAGE);
break;
case DialogInterface.BUTTON_NEGATIVE:
setResult(RESULT_CANCELED);
finish();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(FileExplorerActivity.this);
builder.setTitle(R.string.file_picker_permission_rationale_dialog_title)
.setMessage(R.string.file_picker_permission_rationale_dialog_content)
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
I used the adb to push a file to the emulator:
adb push NOTICE.txt /storage/emulated/0/Download/test.txt