当前位置:K88软件开发文章中心编程语言APP编程Android01 → 文章内容

4.4.2 ContentProvider再探——Document Provider

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-12 6:26:21

Log.e("HeHe", "Size: " + size); } }finally { cursor.close(); }}运行结果:还是那只狗,调用方法后会输入文件名以及文件大小,以byte为单位6)根据Uri获得Bitmap核心代码如下:private Bitmap getBitmapFromUri(Uri uri) throws IOException { ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r"); FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor); parcelFileDescriptor.close(); return image;}运行结果:7)根据Uri获取输入流核心代码如下:private String readTextFromUri(Uri uri) throws IOException { InputStream inputStream = getContentResolver().openInputStream(uri); BufferedReader reader = new BufferedReader(new InputStreamReader( inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } fileInputStream.close(); parcelFileDescriptor.close(); return stringBuilder.toString();}上述的内容只告诉你通过一个Uri你可以知道什么,而Uri的获取则是通过SAF得到的!8) 创建新文件以及删除文件:创建文件:private void createFile(String mimeType, String fileName) { Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType(mimeType); intent.putExtra(Intent.EXTRA_TITLE, fileName); startActivityForResult(intent, WRITE_REQUEST_CODE);}可在onActivityResult()中获取被创建文件的uri删除文件:前提是Document.COLUMN_FLAGS包含SUPPORTS_DELETEDocumentsContract.deleteDocument(getContentResolver(), uri);9)编写一个自定义的Document Provider如果你希望自己应用的数据也能在documentsui中打开,你就需要写一个自己的document provider。下面介绍自定义DocumentsProvider的步骤:API版本为19或者更高在manifest.xml中注册该ProviderProvider的name为类名加包名,比如:com.example.android.storageprovider.MyCloudProviderAuthority为包名+provider的类型名,如:com.example.android.storageprovider.documentsandroid:exported属性的值为ture下面是Provider的例子写法:<manifest... > ... <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" /> .... <provider android:name="com.example.android.storageprovider.MyCloudProvider" android:authorities="com.example.android.storageprovider.documents" android:grantUriPermissions="true" android:exported="true" android:permission="android.permission.MANAGE_DOCUMENTS" android:enabled="@bool/atLeastKitKat"> <intent-filter> <action android:name="android.content.action.DOCUMENTS_PROVIDER" /> </intent-filter> </provider> </application></manifest>10 )DocumentsProvider的子类至少实现如下几个方法:queryRoots()queryChildDocuments()queryDocument()openDocument()还有些其他的方法,但并不是必须的。下面演示一个实现访问文件(file)系统的DocumentsProvider的大致写法。Implement queryRoots@Overridepublic Cursor queryRoots(String[] projection) throws FileNotFoundException { // Create a cursor with either the requested fields, or the default // projection if "projection" is null. final MatrixCursor result = new MatrixCursor(resolveRootProjection(projection)); // If user is not logged in, return an empty root cursor. This removes our // provider from the list entirely. if (!isUserLoggedIn()) { return result; } // It's possible to have multiple roots (e.g. for multiple accounts in the // same app) -- just add multiple cursor rows. // Construct one row for a root called "MyCloud". final MatrixCursor.RowBuilder row = result.newRow(); row.add(Root.COLUMN_ROOT_ID, ROOT); row.add(Root.COLUMN_SUMMARY, getContext().getString(R.string.root_summary)); // FLAG_SUPPORTS_CREATE means at least one directory under the root supports // creating documents. FLAG_SUPPORTS_RECENTS means your application's most // recently used documents will show up in the "Recents" category. // FLAG_SUPPORTS_SEARCH allows users to search all documents the application // shares. row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE | Root.FLAG_SUPPORTS_RECENTS | Root.FLAG_SUPPORTS_SEARCH); // COLUMN_TITLE is the root title (e.g. Gallery, Drive). row.add(Root.COLUMN_TITLE, getContext().getString(R.string.title)); // This document id cannot change once it's shared. row.add(Root.COLUMN_DOCUMENT_ID, getDocIdForFile(mBaseDir)); // The child MIME types are used to filter the roots and only present to the // user roots that contain the desired type somewhere in their file hierarchy. row.add(Root.COLUMN_MIME_TYPES, getChildMimeTypes(mBaseDir)); row.add(Root.COLUMN_AVAILABLE_BYTES, mBaseDir.getFreeSpace()); row.add(Root.COLUMN_ICON, R.drawable.ic_launcher); return result;}Implement queryChildDocumentspublic Cursor queryChildDocuments(String parentDocumentId, String

上一页  [1] [2] [3] [4]  下一页


4.4.2 ContentProvider再探——Document Provider