下面列出了android.content.Intent#ACTION_OPEN_DOCUMENT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
*/
private boolean importThemes( Context context )
{
if (context != null)
{
if (isImporting || isExporting) {
Log.e("importThemes","Busy! Already importing/exporting.. ignoring request");
return false;
} else {
Intent intent = new Intent((Build.VERSION.SDK_INT >= 19 ? Intent.ACTION_OPEN_DOCUMENT : Intent.ACTION_GET_CONTENT));
intent.setType("text/*");
startActivityForResult(intent, IMPORT_REQUEST);
return true;
}
}
return false;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.apk:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/vnd.android.package-archive");
startActivityForResult(intent, REQUEST_READ_APK);
break;
case R.id.sort:
mIsSortByTime = !mIsSortByTime;
item.setIcon(mIsSortByTime ? R.drawable.ic_a_white_24dp : R.drawable.ic_timer_white_24dp);
mRefreshView.setRefreshing(true);
mToolbar.getMenu().getItem(1).setEnabled(false);
mDisposables.add(mPresenter.getAndSort(this, mIsSortByTime));
break;
}
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.reset_icon:
File customIconFile = MyCache.getCustomIconFile(this, component);
customIconFile.delete();
Toast.makeText(ChangeIconActivity.this, "The custom icon was deleted", Toast.LENGTH_LONG).show();
((Button)findViewById(R.id.reset_icon)).setEnabled(false);
break;
case R.id.choose_icon_from_memory:
if (Build.VERSION.SDK_INT >= 19) {
Intent customIconIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
customIconIntent.addCategory(Intent.CATEGORY_OPENABLE);
customIconIntent.setType("image/png");
startActivityForResult(customIconIntent, 0);
} else {
Intent intent = new Intent(this, FileActivity.class);
startActivityForResult(intent, FileActivity.GET_IMAGE);
}
break;
}
}
public void folderImageButtonClick() {
if (Pref.getString(PREF_numberwall_background, null) == null) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
if (SdcardImportExport.checkPermissions(activity, true, ASK_FILE_PERMISSION)) {
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, LOAD_IMAGE_RESULTS);
} else {
if (JoH.ratelimit("need-file-permission",10)) {
//JoH.static_toast_short("Need file permission");
}
}
}
} else {
binding.getSprefs().put(PREF_numberwall_background, null);
refreshBitmap();
}
}
public void pickImage() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType("image/*"),
REQUEST_PICK_IMAGE);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_SAF_PICK_IMAGE);
}
}
public void openDocument() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(MIME_TYPE);
intent.putExtra(EXTRA_SHOW_ADVANCED, true);
this.getActionHandler().callStartActivityForResult(intent, new TGSafOpenHandler(this));
}
/**
* 4.4以上版本使用
* @see "http://blog.csdn.net/tempersitu/article/details/20557383"
*
* @param activity
* @param requestCode
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void choicePicFromAlbum_kitkat(Activity activity, int requestCode) {
// 来自相册
Intent albumIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
albumIntent.addCategory(Intent.CATEGORY_OPENABLE);
albumIntent.setType("image/*");
activity.startActivityForResult(albumIntent, requestCode);
}
/**
* 获取存储访问框架的意图
* @param type 跳转类型
* @return 存储访问框架的意图
*/
public static Intent getOpenDocumentIntent(final String type) {
try {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(type);
return intent;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "getOpenDocumentIntent");
}
return null;
}
@NeedsPermission(Manifest.permission.READ_EXTERNAL_STORAGE) public void pickImage() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType("image/*"),
REQUEST_PICK_IMAGE);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_SAF_PICK_IMAGE);
}
}
/**
* Get the Intent for selecting content to be used in an Intent Chooser.
*
* @return The intent for opening a file with Intent.createChooser()
* @author paulburke
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
private static Intent createGetMultipleContentIntent(String mimeType) {
// Implicitly allow the user to select a particular kind of data
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
// The MIME data type filter
intent.setType(mimeType);
// EXTRA_ALLOW_MULTIPLE: added in API level 18
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
// Only return URIs that can be opened with ContentResolver
intent.addCategory(Intent.CATEGORY_OPENABLE);
return intent;
}
/**
* 4.4以上版本使用
* @see "http://blog.csdn.net/tempersitu/article/details/20557383"
*
* @param activity
* @param requestCode
*/
public static void choicePicFromAlbum_kitkat(Activity activity, int requestCode) {
// 来自相册
Intent albumIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
albumIntent.addCategory(Intent.CATEGORY_OPENABLE);
albumIntent.setType("image/*");
activity.startActivityForResult(albumIntent, requestCode);
}
public static Intent pickFile(boolean onlyImages) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
if (onlyImages) {
intent.setType("image/*");
} else {
intent.setType("*/*");
}
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
return Intent.createChooser(intent, "Select file");
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch(item.getItemId()) {
case R.id.action_start_selection:
listener.startMultiSelect();
return true;
case R.id.action_settings:
Intent intentSettings = new Intent(getActivity(), SettingsActivity.class);
startActivity(intentSettings);
return true;
case R.id.action_import:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"text/plain", "text/html", "text/x-markdown"});
intent.setType("*/*");
try {
getActivity().startActivityForResult(intent, MainActivity.IMPORT);
} catch (ActivityNotFoundException e) {
showToast(R.string.error_importing_notes);
}
return true;
case R.id.action_about:
DialogFragment aboutFragment = new AboutDialogFragment();
aboutFragment.show(getFragmentManager(), "about");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* 获取导入规则列表的 SAF (Storage Access Framework) 的 Intent
*/
public static Intent getImportRuleListSAFIntent() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(BACKUP_MIME_TYPE);
intent.putExtra(Intent.EXTRA_TITLE, getDefaultBackupFilename());
return intent;
}
private void pickImage(Activity a) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
a.startActivityForResult(intent, PICK_IMAGE_REQUEST_CODE);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.main_menu_freeze_all:
// This is the same as clicking on the batch freeze shortcut
// so we just forward the request to DummyActivity
Intent intent = new Intent(DummyActivity.PUBLIC_FREEZE_ALL);
intent.setComponent(new ComponentName(this, DummyActivity.class));
startActivity(intent);
return true;
case R.id.main_menu_settings:
Intent settingsIntent = new Intent(this, SettingsActivity.class);
Bundle extras = new Bundle();
extras.putBinder("profile_service", mServiceWork.asBinder());
settingsIntent.putExtra("extras", extras);
startActivity(settingsIntent);
return true;
case R.id.main_menu_create_freeze_all_shortcut:
Intent launchIntent = new Intent(DummyActivity.PUBLIC_FREEZE_ALL);
launchIntent.setComponent(new ComponentName(this, DummyActivity.class));
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Utility.createLauncherShortcut(this, launchIntent,
Icon.createWithResource(this, R.mipmap.ic_freeze),
"shelter-freeze-all", getString(R.string.freeze_all_shortcut));
return true;
case R.id.main_menu_install_app_to_profile:
Intent openApkIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
openApkIntent.addCategory(Intent.CATEGORY_OPENABLE);
openApkIntent.setType("application/vnd.android.package-archive");
startActivityForResult(openApkIntent, REQUEST_DOCUMENTS_CHOOSE_APK);
return true;
case R.id.main_menu_show_all:
Runnable update = () -> {
mShowAll = !item.isChecked();
item.setChecked(mShowAll);
LocalBroadcastManager.getInstance(this)
.sendBroadcast(new Intent(AppListFragment.BROADCAST_REFRESH));
};
if (!item.isChecked()) {
new AlertDialog.Builder(this)
.setMessage(R.string.show_all_warning)
.setPositiveButton(R.string.first_run_alert_continue,
(dialog, which) -> update.run())
.setNegativeButton(R.string.first_run_alert_cancel, null)
.show();
} else {
update.run();
}
return true;
}
return super.onOptionsItemSelected(item);
}
public void loadPicture() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_FILE);
}
private void importNotebook(){
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/zip");
startActivityForResult(intent, Utils.READ_REQUEST_CODE);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, Utils.READ_REQUEST_CODE);
}
/**
* ファイル読み込み用のUriを要求するヘルパーメソッド
* KITKAT以降で個別のファイル毎にパーミッション要求する場合
* @param mime
* @return
*/
private static Intent prepareOpenDocumentIntent(@NonNull final String mime) {
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType(mime);
return intent;
}