StorageClient
来源:互联网 发布:手机歌录软件 编辑:程序博客网 时间:2024/06/07 11:11
路径
...\sdk\samples\android-23\content\StorageClient
描述
Storage Access Framework(SAF),Android 4.4 (API level 19) 后引入的一个功能,可以打开一个类似文件选择器的界面,通过setType()来设置需要选择什么类型的文件。此次示例打开的是图片选择器
知识点
/** * Fires an intent to spin up the "file chooser" UI and select an image. */ public void performFileSearch() { // BEGIN_INCLUDE (use_open_document_intent) // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file browser. Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); // Filter to only show results that can be "opened", such as a file (as opposed to a list // of contacts or timezones) intent.addCategory(Intent.CATEGORY_OPENABLE); // Filter to show only images, using the image MIME data type. // If one wanted to search for ogg vorbis files, the type would be "audio/ogg". // To search for all documents available via installed storage providers, it would be // "*/*". intent.setType("image/*"); startActivityForResult(intent, READ_REQUEST_CODE); // END_INCLUDE (use_open_document_intent) }
通过Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
创建一个Intent,再添加intent.addCategory(Intent.CATEGORY_OPENABLE);
,最后设置打开类型intent.setType("image/*");
如果要过滤文本可以设置 intent.setType("text/*");
不过滤
intent.setType("*/*");
使用startActivityForResult(intent, READ_REQUEST_CODE);
来启动。
在onActivityResult中接收返回的结果
@Override public void onActivityResult(int requestCode, int resultCode, Intent resultData) { Log.i(TAG, "Received an \"Activity Result\""); // BEGIN_INCLUDE (parse_open_document_response) // The ACTION_OPEN_DOCUMENT intent was sent with the request code READ_REQUEST_CODE. // If the request code seen here doesn't match, it's the response to some other intent, // and the below code shouldn't run at all. if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) { // The document selected by the user won't be returned in the intent. // Instead, a URI to that document will be contained in the return intent // provided to this method as a parameter. Pull that uri using "resultData.getData()" Uri uri = null; if (resultData != null) { uri = resultData.getData(); Log.i(TAG, "Uri: " + uri.toString()); showImage(uri); } // END_INCLUDE (parse_open_document_response) } }
如果返回结果不为空,展示这张图片
if (resultData != null) { uri = resultData.getData(); Log.i(TAG, "Uri: " + uri.toString()); showImage(uri);}/** * Given the URI of an image, shows it on the screen using a DialogFragment. * * @param uri the Uri of the image to display. */ public void showImage(Uri uri) { // BEGIN_INCLUDE (create_show_image_dialog) if (uri != null) { // Since the URI is to an image, create and show a DialogFragment to display the // image to the user. FragmentManager fm = getActivity().getSupportFragmentManager(); ImageDialogFragment imageDialog = new ImageDialogFragment(); Bundle fragmentArguments = new Bundle(); fragmentArguments.putParcelable("URI", uri); imageDialog.setArguments(fragmentArguments); imageDialog.show(fm, "image_dialog"); } // END_INCLUDE (create_show_image_dialog) }
这里使用了DialogFragment。
0 0