SelectFile Library for Processing Android Mode
Android has no default Open & Save file dialogs.
SelectFile
library for Processing provides UI dialogs for these methods in Android mode:
selectInput()
- select any file
selectFolder()
- select any folder
selectOutput()
- save file
Tested with Processing 3.0.
|
|
All SelectFile features |
.selectFile() in action |
Check library examples to see related code.
Installation into PDE
- In Processing go to
Sketches > Import Library > Add Library...
- Search for
SelectFile
- Click install.
Downloads or Installation into APDE
If you want to install SelectFile manually, you can find the latest binary package here:
http://andrusiv.com/android-select-file/download/SelectFile.zip.
If you're using APDE app, please download from the link mentioned above and import compressed library (.zip file) into APDE.
Usage
- After you installed the library, check out an example here:
File > Examples > Contributed Libraries > SelectFile
.
- Add correct READ/WRITE permissions to your sketch. Go to
Android > Sketch Permissions
and choose READ_EXTERNAL_STORAGE
and WRITE_EXTERNAL_STORAGE
.
Building From Source
- Install
ant
. On Mac OS X: brew install ant
cd resources
ant
SelectFile
will appear in your Sketchbooks folder. On Mac OS X the default location is ~/Documents/Processing
.
Using in Pure Android
The code responsible for selecting files does not depend on Processing and can be reused in other Android projects.
Look into SelectLibrary
class.
Here's an example, how one can use the library from Android:
`MainActivity.java` source code.
Activity with three buttons and a text field. When someone accomplishes
selected action (select file, folder or save file) text field changes
its text to the selected path.
```java
public class MainActivity extends Activity {
// define a constant for activity result
protected static final int PATH_RESULT = 123;
private TextView tv = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SelectActivity.class);
i.putExtra(SelectActivity.EX_PATH, Environment.getExternalStorageDirectory().getAbsolutePath());
i.putExtra(SelectActivity.EX_STYLE, SelectMode.SELECT_FILE);
startActivityForResult(i, PATH_RESULT);
}
});
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SelectActivity.class);
i.putExtra(SelectActivity.EX_PATH, Environment.getExternalStorageDirectory().getAbsolutePath());
i.putExtra(SelectActivity.EX_STYLE, SelectMode.SELECT_FOLDER);
startActivityForResult(i, PATH_RESULT);
}
});
b3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SelectActivity.class);
i.putExtra(SelectActivity.EX_PATH, Environment.getExternalStorageDirectory().getAbsolutePath());
i.putExtra(SelectActivity.EX_STYLE, SelectMode.SAVE_FILE);
startActivityForResult(i, PATH_RESULT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PATH_RESULT && resultCode == RESULT_OK) {
tv.setText(data.getStringExtra(SelectActivity.EX_PATH_RESULT));
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
```
`activity_main.xml` activity layout source.
Layout for the activity:
```xml
```
@author ostap.andrusiv