-
Notifications
You must be signed in to change notification settings - Fork 27
/
MainActivity.java
118 lines (88 loc) · 3.68 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package lib.folderpicker.demo;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import lib.folderpicker.FolderPicker;
public class MainActivity extends AppCompatActivity {
private static final int SDCARD_PERMISSION = 1,
FOLDER_PICKER_CODE = 2,
FILE_PICKER_CODE = 3;
TextView tvFolder, tvFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkStoragePermission();
initUI();
}
void checkStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//Write permission is required so that folder picker can create new folder.
//If you just want to pick files, Read permission is enough.
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
SDCARD_PERMISSION);
}
}
}
void initUI() {
findViewById(R.id.btn_folder).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pickFolder();
}
});
findViewById(R.id.btn_file).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pickFile();
}
});
tvFolder = (TextView) findViewById(R.id.tv_folder);
tvFile = (TextView) findViewById(R.id.tv_file);
}
void pickFolder() {
Intent intent = new Intent(this, FolderPicker.class);
startActivityForResult(intent, FOLDER_PICKER_CODE);
}
void pickFile() {
Intent intent = new Intent(this, FolderPicker.class);
//Optional
intent.putExtra("title", "Select file to upload");
intent.putExtra("location", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
intent.putExtra("pickFiles", true);
//Optional
startActivityForResult(intent, FILE_PICKER_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == FOLDER_PICKER_CODE) {
if (resultCode == Activity.RESULT_OK && intent.hasExtra("data")) {
String folderLocation = "<b>Selected Folder: </b>"+ intent.getExtras().getString("data");
tvFolder.setText( Html.fromHtml(folderLocation) );
} else if (resultCode == Activity.RESULT_CANCELED) {
tvFolder.setText(R.string.folder_pick_cancelled);
}
} else if (requestCode == FILE_PICKER_CODE) {
if (resultCode == Activity.RESULT_OK && intent.hasExtra("data")) {
String fileLocation = "<b>Selected File: </b>"+ intent.getExtras().getString("data");
tvFile.setText( Html.fromHtml(fileLocation) );
} else if (resultCode == Activity.RESULT_CANCELED) {
tvFile.setText(R.string.file_pick_cancelled);
}
}
}
}