/** Copyright (C) The Android Open Source project** Licensed under the Apache License, Version 2.0 (the "License"); * Except in compliance with the license.* your may obtain a copy of the License at** HTTP://WW w.apache.org/licenses/license-2.0** unless required by applicable law or agreed to in writing, software* distributed under The License is distributed on a "as is" basis,* without warranties or CONDITIONS of any KIND, either express or implied. * See the License for the specific language governing permissions and* limitations under the License.*/package com.example . Android.justforus;import Android.content.contentprovider;import Android.content.contentvalues;import Android.content.context;import Android.content.res.assetfiledescriptor;import Android.content.res.AssetManager; Import Android.database.cursor;import Android.database.matrixcursor;import Android.net.uri;import Android.provider.openablecolumns;import Android.util.log;import Java.io.FileNotFoundexception;import java.io.ioexception;import Static java.net.urlconnection.guesscontenttypefromname;/** * Generic content provider, which makes any files available in this app ' s "assets" directory * available publicly. * * <p>to use, add the following to your androidmanifest.xml: * * <code><pre> * <provider * Android : Name= ". Assetprovider "* android:authorities=" [YOUR CONTENT PROVIDER DOMAIN here] "* android:granturipermissions=" true "* an Droid:exported= "true"/> * </pre></code> */public class Assetprovider extends ContentProvider {/** * Content Provider authority that identifies data, is offered by this * {@link assetprovider}. */public static String Content_uri = "Com.example.android.justforus"; private static final String TAG = "Assetprovider"; Assetmanager massets; @Override public boolean onCreate () {Context CTX = GetContext (); if (CTX = = null) {//Context not available. Give up. return false; } massets = Ctx.getassets (); return true; } @Override public String getType (URI uri) {//Returns the MIME type for the selected URI, in conformance wit H the ContentProvider//interface. Looks up the file indicated By/res/assets/{uri.path}, and returns the MIME//type for that file as guessed by the URLConnection class. Setup String Path = Uri.getlastpathsegment (); Check if file exists if (!fileexists (path)) {return null; }//determine mime-type based on filename return guesscontenttypefromname (uri.tostring ()); } @Override public assetfiledescriptor openassetfile (URI Uri, String mode) throws FileNotFoundException, SecurityException {//ContentProvider interface for opening a file descriptor by URI. This content provider//maps all URIs to the contents of the APK "s assets folder, so a file handle to /res/assets/{uri.path} 'll be returned. Security check. This content provider only supports read-only access. (Also, the contents//of an APKs assets folder is immutable, so read-write access doesn ' t make sense here.) if (! " R ". Equals (Mode)) {throw new SecurityException (" Read-only access is supported, mode must be [r] "); }//Open asset from within APK and return file descriptor String Path = Uri.getlastpathsegment (); try {return massets.openfd (path); } catch (IOException e) {throw new FileNotFoundException (); }}/** * Check If file exists inside APK assets. * * @param path Fully qualified path to file. * @return True if exists, false otherwise. */Private Boolean fileexists (String path) {try {//Check to see if file can is opened. If so, file exists. MASSETS.OPENFD (Path). Close (); return true; } catch (IOException e) {//Unable to open file descriptor for specified path; file doesn ' t exist. return false; }} @Override Public Cursor query (URI uri, string[] projection, String selection, string[] Selectionargs, String sortOrder) {String path = Uri.getlastpathsegment (); if (!fileexists (path)) {LOG.E (TAG, "requested file doesn ' t exist at" + path); return null; }//Create matrix cursor if (projection = = null) {projection = new string[]{ Openablecolumns.display_name, Openablecolumns.size,}; } matrixcursor matrixcursor = new Matrixcursor (projection, 1); object[] row = new Object[projection.length]; for (int col = 0; col < projection.length; col++) {if (OpenableColumns.DISPLAY_NAME.equals (Projection[col]) ) {Row[col] = path; } else if (OpenableColumns.SIZE.equals (Projection[col])) {try {assetfiledescriptor afd = ope Nassetfile (URI, "R"); if (afd! = null) {Row[col] = long.valueof (Afd.getlength ()); } afd.close (); } catch (IOException e) {log.e (TAG, e.tostring ()); }}} matrixcursor.addrow (row); return matrixcursor; }//Required/unused ContentProvider methods below. @Override public uri Insert (URI Uri, contentvalues contentvalues) {throw new RuntimeException (' Operation not Su Pported "); } @Override public int delete (URI Uri, String selection, string[] Selectionargs) {throw new runtimeexception ("Operation not supported"); } @Override public int update (URI uri, contentvalues values, String selection, string[] selectionargs) {thro W New RuntimeException ("Operation not SUPPorted "); }}
Android Assert Tool Class