android添加图片资源文件,android共享资源文件夹中的图像

public class AssetsProvider extends ContentProvider {

private final static String LOG_TAG = AssetsProvider.class.getName();

private static final String[] COLUMNS = {

OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE };

@Override

public boolean onCreate() {

return true;

}

@Override

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

/**

* Source: {@link FileProvider#query(Uri, String[], String, String[], String)} .

*/

if (projection == null) {

projection = COLUMNS;

}

final AssetManager am = getContext().getAssets();

final String path = getRelativePath(uri);

long fileSize = 0;

try {

final AssetFileDescriptor afd = am.openFd(path);

fileSize = afd.getLength();

afd.close();

} catch(IOException e) {

Log.e(LOG_TAG, \"Can\'t open asset file\", e);

}

final String[] cols = new String[projection.length];

final Object[] values = new Object[projection.length];

int i = 0;

for (String col : projection) {

if (OpenableColumns.DISPLAY_NAME.equals(col)) {

cols[i] = OpenableColumns.DISPLAY_NAME;

values[i++] = uri.getLastPathSegment();

} else if (OpenableColumns.SIZE.equals(col)) {

cols[i] = OpenableColumns.SIZE;

values[i++] = fileSize;

}

}

final MatrixCursor cursor = new MatrixCursor(cols, 1);

cursor.addRow(values);

return cursor;

}

@Override

public String getType(Uri uri) {

/**

* Source: {@link FileProvider#getType(Uri)} .

*/

final String file_name = uri.getLastPathSegment();

final int lastDot = file_name.lastIndexOf(\'.\');

if (lastDot >= 0) {

final String extension = file_name.substring(lastDot + 1);

final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);

if (mime != null) {

return mime;

}

}

return \"application/octet-stream\";

}

@Override

public Uri insert(Uri uri, ContentValues values) {

return null;

}

@Override

public int delete(Uri uri, String selection, String[] selectionArgs) {

return 0;

}

@Override

public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

return 0;

}

@Override

public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {

final AssetManager am = getContext().getAssets();

final String path = getRelativePath(uri);

if(path == null) {

throw new FileNotFoundException();

}

AssetFileDescriptor afd = null;

try {

afd = am.openFd(path);

} catch(IOException e) {

Log.e(LOG_TAG, \"Can\'t open asset file\", e);

}

return afd;

}

private String getRelativePath(Uri uri) {

String path = uri.getPath();

if (path.charAt(0) == \'/\') {

path = path.substring(1);

}

return path;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值