Android Assert工具类
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "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"
* android:exported="true"/>
* </pre></code>
*/
public class AssetProvider extends ContentProvider { /**
* Content provider authority that identifies data that 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 with 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} will be returned. // Security check. This content provider only supports read-only access. (Also, the contents
// of an APKs assets folder are immutable, so read-write access doesn't make sense here.)
if (!"r".equals(mode)) {
throw new SecurityException("Only 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 be 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 = openAssetFile(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 supported");
} @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) {
throw new RuntimeException("Operation not supported");
}
}
Android Assert工具类的更多相关文章
- 53. Android常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...
- Android 常见工具类封装
1,MD5工具类: public class MD5Util { public final static String MD5(String s) { char hexDigits[] = { '0' ...
- 【转】Android常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefe ...
- Spring的Assert工具类的用法
简介 今天在看spring mvc源码时看到下面代码,感觉蛮有意思的,在这里记录下 Assert断言工具类,通常用于数据合法性检查,在JAVA编程中,通常会编写如下代码: if (name == nu ...
- Android基础工具类重构系列一Toast
前言: 一直在考虑写一下Android实际项目中的一些总结,翻看CSDN博客,上一篇已经是一年多曾经. 本系列定位Android基础工具类重构.旨在记录实际项目中经经常使用到的一些工具类,比方Toas ...
- (转载)android 一些工具类汇总
android 一些工具类汇总 作者:曾田生z 字体:[增加 减小] 类型:转载 时间:2016-08-14我要评论 本文给大家汇总介绍了一些常用的Android工具类,非常的简单实用,有需要的小伙伴 ...
- 随笔分类 - Android之工具类
Android之文件搜索工具类 /** * @detail 搜索sdcard文件 * @param 需要进行文件搜索的目录 * @param 过滤搜索文件类型 */ private void sear ...
- Android 系统工具类SystemUtils
包含的功能有: 获取系统中所有APP应用.获取用户安装的APP应用.根据包名和Activity启动类查询应用信息.跳转到WIFI设置.WIFI网络开关.移动网络开关.GPS开关 当前若关则打开 当前若 ...
- Android Sqlite 工具类封装
鉴于经常使用 Sqlite 数据库做数据持久化处理,进行了一点封装,方便使用. 该封装类主要支持一下功能 支持多用户数据储存 支持 Sqlite数据库升级 支持传入 Sql 语句建表 支持 SQLit ...
随机推荐
- LinearLayout的一些注意事项
1.orientation的默认值为horizontal,即从左向右排列.由于一般从上向下排列,所以必须指定orientation属性. 2.layout_gravity与gravity的区别: (1 ...
- putty设置
1- 输入要链接的主机地址 2- 设置connection-->SSH-->Tunnels 点击Add 3- 设置connection 修改为30 4- 点击open,出现ssh登陆,输入 ...
- 如何调用EcStore中的API接口
EcStore系统已内置了丰富的API接口供外部系统调用(接口列表见文章最下面),外部系统具体如何调用这些API呢? 例如有一个PHP的论坛需要调用ecstore系统内一个商品的详情,则可以使用b2c ...
- 用php逐行读取文件
做个备份年纪大了,都不愿意自己思考了 $str = file_get_contents($tmpfilename);//获得内容 $arr = explode("\n",$str) ...
- Histats申请Counter网站计数器 - Blog透视镜
为了计算网页被浏览的次数,访客人数等统计数据,作为未来分析之用,可以向Histats申请免费的Counter网站计数器,它的功能相当齐全,同时也会保留一段时间的资料,当作统计比较的资料,更可以进一步付 ...
- usb开发笔记
U盘应属于海量存储类. USB海量存储设备,又包括通用海量存储子类,CDROM,Tape等,U盘实际上属于海量存储类中通用海量存储子类.通用海量存储设备实现上是基于块/扇区存储的设备. USB组织定义 ...
- Redhat关闭SELinux和防火墙的办法(转)
Redhat使用了SELinux来增强安全,关闭的办法为:1. 永久有效修改 /etc/selinux/config 文件中的 SELINUX="" 为 disabled ,然后重 ...
- 如何导出sqlserver中的表数据,sqlserver2008
sqlserver数据库中的表数据,我们通常想使用一下,可是怎样获取这些数据呢? 1.选择任务->生成脚本 2.选择数据库 3.设置配置,让编写数据的脚本为true 4.保存为sql 5.完成 ...
- 【排序】表插入排序算法(C语言版)
排序耗时的操作主要分为两种:查找比较.记录移位. 1.表插入排序 在查找比较基础上,尽量减少记录移位步数,可以令排序操作耗时降低,表插入排序正是为减少移位次数而出现的. 在数据结构上,数据是存储在静态 ...
- UESTC_The Most Wonderful Competition CDOJ 56
The Most Wonderful Competition Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB ...