下面列出了android.content.Intent#ACTION_OPEN_DOCUMENT_TREE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void exportNotes() {
filesToExport = cab.toArray();
cab.clear();
if(filesToExport.length == 1 || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
fileBeingExported = 0;
reallyExportNotes();
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
try {
startActivityForResult(intent, EXPORT_TREE);
} catch (ActivityNotFoundException e) {
showToast(R.string.error_exporting_notes);
}
}
}
private static Intent getFolderPickerIntent(@NonNull final Context context) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
intent.putExtra(DocumentsContract.EXTRA_PROMPT, "Allow Write Permission");
}
// http://stackoverflow.com/a/31334967/1615876
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
// Start the SAF at the specified location
if (Build.VERSION.SDK_INT >= O && !Preferences.getStorageUri().isEmpty()) {
DocumentFile file = DocumentFile.fromTreeUri(context, Uri.parse(Preferences.getStorageUri()));
if (file != null)
intent.putExtra(EXTRA_INITIAL_URI, file.getUri());
}
HentoidApp.LifeCycleListener.disable(); // Prevents the app from displaying the PIN lock when returning from the SAF dialog
return intent;
}
/**
* Invoked after user clicked on the {@link mPathView} label.
*/
@SuppressLint("InlinedAPI")
private void onPathViewClick() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
startActivityForResult(FolderPickerActivity.createIntent(this, mFolder.path, null),
FolderPickerActivity.DIRECTORY_REQUEST_CODE);
return;
}
// This has to be android.net.Uri as it implements a Parcelable.
android.net.Uri externalFilesDirUri = FileUtils.getExternalFilesDirUri(FolderActivity.this);
// Display storage access framework directory picker UI.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
if (externalFilesDirUri != null) {
intent.putExtra("android.provider.extra.INITIAL_URI", externalFilesDirUri);
}
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
try {
startActivityForResult(intent, CHOOSE_FOLDER_REQUEST);
} catch (android.content.ActivityNotFoundException e) {
Log.e(TAG, "onPathViewClick exception, falling back to built-in FolderPickerActivity.", e);
startActivityForResult(FolderPickerActivity.createIntent(this, mFolder.path, null),
FolderPickerActivity.DIRECTORY_REQUEST_CODE);
}
}
private void onStoreAttachments(Intent intent) {
message = intent.getLongExtra("id", -1);
Intent tree = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
Helper.openAdvanced(tree);
PackageManager pm = getContext().getPackageManager();
if (tree.resolveActivity(pm) == null) // system whitelisted
ToastEx.makeText(getContext(), R.string.title_no_saf, Toast.LENGTH_LONG).show();
else
startActivityForResult(Helper.getChooser(getContext(), tree), REQUEST_ATTACHMENTS);
}
private void selectHentoidFolder() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
intent.putExtra(DocumentsContract.EXTRA_PROMPT, "Allow Write Permission");
}
// http://stackoverflow.com/a/31334967/1615876
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, RQST_STORAGE_PERMISSION);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void showSystemDialog()
{
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, REQUEST_CODE_ADD_LOCATION);
Toast.makeText(getActivity(), R.string.select_root_folder_tip, Toast.LENGTH_LONG).show();
}
private void requestBackupAccess() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
| Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && settings.isBackupLocationSet())
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, settings.getBackupLocation());
startActivityForResult(intent, Constants.INTENT_SETTINGS_BACKUP_LOCATION);
}
public static Intent getDocTreeIntent()
{
if (isMarshMallowOrLater())
{
return new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
}
return null;
}
/***
* Request storage access. The user needs to press "Select storage" at the correct storage.
* @param activity The activity which will receive the result from startActivityForResult
*/
public void requestStorageAccessFramework(final Activity... activity) {
Activity a = greedyGetActivity(activity);
if (a != null && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
| Intent.FLAG_GRANT_PREFIX_URI_PERMISSION
);
a.startActivityForResult(intent, REQUEST_SAF);
}
}
private void selectBackupsLocation() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
| Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
startActivityForResult(intent, CODE_BACKUPS);
}
/***
* Request storage access. The user needs to press "Select storage" at the correct storage.
* @param activity The activity which will receive the result from startActivityForResult
*/
public void requestStorageAccessFramework(final Activity... activity) {
Activity a = greedyGetActivity(activity);
if (a != null && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
| Intent.FLAG_GRANT_PREFIX_URI_PERMISSION
);
a.startActivityForResult(intent, REQUEST_SAF);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void openDirPickerL() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
try {
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE_DIR_L);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), R.string.em_cant_find_activity, Toast.LENGTH_SHORT).show();
}
}
public void requestUriAccessIntentInCurrentThread() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.putExtra(EXTRA_SHOW_ADVANCED, true);
TGSafBrowserUriResult handler = new TGSafBrowserUriResult(this);
this.callStartActivityForResult(intent, handler.getRequestCode());
}
private static void triggerStorageAccessFramework(final ThemedActivity mainActivity) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
mainActivity.startActivityForResult(intent, ThemedActivity.FROM_PREVIOUS_IO_ACTION);
}
@Override
protected void addNewLocation(String locationType)
{
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, REQUEST_CODE_ADD_LOCATION);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void openTreePicker(Activity activity) {
Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
i.putExtra("android.content.extra.SHOW_ADVANCED", true);
activity.startActivityForResult(i, SAFUtil.REQUEST_SAF_PICK_TREE);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void openTreePicker(Fragment fragment) {
Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
i.putExtra("android.content.extra.SHOW_ADVANCED", true);
fragment.startActivityForResult(i, SAFUtil.REQUEST_SAF_PICK_TREE);
}
/**
* Trigger the storage access framework to access the base folder of the ext sd card.
*
* @param code The request code to be used.
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private void triggerStorageAccessFramework(final int code) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, code);
}
/**
* requestStorageAccessの下請け
* @return
*/
private static Intent prepareStorageAccessPermission() {
return new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
}
/**
* requestStorageAccessの下請け
* ドキュメントツリーへのアクセスのためのIntentを返す
* @return
*/
private static Intent prepareStorageAccessPermission() {
return new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
}