介绍一下 Android 中的 ContentUris。
时间: 2024-02-23 09:02:53 浏览: 16
ContentUris 是 Android 中的一个类,它提供了方便的方法来处理标识 ContentProvider 中数据的 URI。它允许您构建针对提供程序中特定行数据的 URI,并从这些 URI 中提取信息。
ContentUris 类中有许多静态方法,例如 withAppendedId()、parseId()、parseIdList() 等,这些方法可以帮助您添加标识符、获取标识符或解析标识符列表。使用 ContentUris 类可以更方便地操作 ContentProvider 中的数据,例如对数据进行插入、更新、删除或查询等操作。
例如,使用 ContentUris.withAppendedId() 方法可以将 ID 添加到 URI 中:
```
long rowId = ...;
Uri uri = ContentUris.withAppendedId(CONTENT_URI, rowId);
```
这将在 CONTENT_URI 后添加 rowId,以便访问特定行的数据。如果您想获取 URI 中的 ID,可以使用 ContentUris.parseId() 方法:
```
long rowId = ContentUris.parseId(uri);
```
这将从 URI 中提取 ID 并返回。这些方法可以帮助您更轻松地处理 ContentProvider 中的数据。
相关问题
android通过ContentUris.withAppendedId获取的uri转成string格式的图片
获取到的 `Uri` 可以通过 `getContentResolver().openInputStream()` 方法获取到 `InputStream`,然后可以将 `InputStream` 中的数据转换成 `Bitmap`,最后将 `Bitmap` 转换成字符串格式。示例代码如下:
```java
Uri imageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageId);
try {
InputStream inputStream = getContentResolver().openInputStream(imageUri);
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
String imageString = Base64.encodeToString(byteArray, Base64.DEFAULT);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
```
这里的 `imageId` 是图片在 MediaStore 中的 id,可以通过查询 MediaStore.Images.Media.EXTERNAL_CONTENT_URI 来获取。
android11从uri中读取path的方法
在 Android 11 及以上版本中,由于限制了对外部存储的访问,从 URI 中读取文件路径的方法发生了变化,可以使用以下方法:
```java
public String getFilePathFromUri(Context context, Uri uri) {
String filePath = "";
if (DocumentsContract.isDocumentUri(context, uri)) {
// 如果是Document类型的URI,则通过Document ID获取文件路径
String documentId = DocumentsContract.getDocumentId(uri);
if (uri.getAuthority().equals("com.android.externalstorage.documents")) {
String[] split = documentId.split(":");
String type = split[0];
if (type.equals("primary")) {
filePath = Environment.getExternalStorageDirectory() + "/" + split[1];
} else {
filePath = "/storage/" + type + "/" + split[1];
}
} else if (uri.getAuthority().equals("com.android.providers.downloads.documents")) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.parseLong(documentId));
filePath = getDataColumn(context, contentUri, null, null);
} else if (uri.getAuthority().equals("com.android.providers.media.documents")) {
String[] split = documentId.split(":");
String type = split[0];
Uri contentUri = null;
if (type.equals("image")) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if (type.equals("video")) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if (type.equals("audio")) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
String selection = MediaStore.Images.Media._ID + "=?";
String[] selectionArgs = new String[]{split[1]};
filePath = getDataColumn(context, contentUri, selection, selectionArgs);
}
} else if (uri.getScheme().equals("file")) {
filePath = uri.getPath();
}
return filePath;
}
private String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
String column = "_data";
String[] projection = {column};
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
```
在上述代码中,使用了 `DocumentsContract` 类来处理不同类型的 URI,同时还使用了 `Environment.getExternalStorageDirectory()` 方法来获取外部存储器的路径。由于这种方法仅适用于 Android 11 及以上版本,因此需要在代码中进行版本判断。