Chr*_*Zou 2 java android uri mediastore android-contentresolver
我在MediaStore中获得了图片ID.如何在ID中查看图库中的图像?我目前使用以下代码:
ContentResolver cr = context.getContentResolver();
String columns[] = new String[]{
Media._ID, Media.DATA
};
Cursor cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, Media._ID+"=?", new String[]{id+""}, null);
if(cursor.moveToNext()) {
String imagePath = cursor.getString(cursor.getColumnIndex(Media.DATA));
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.addCategory(android.content.Intent.CATEGORY_DEFAULT);
intent.setDataAndType(imageUri, "image/*");
((Activity)context).startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
它有效,但它太麻烦了.我认为应该有一种更简单的方法来做到这一点.关键是从图像ID获取图像Uri.
Tri*_*ode 13
int imageID = cursor.getInt( cursor.getColumnIndex(MediaStore.Images.Media._ID) );
Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
Run Code Online (Sandbox Code Playgroud)
要从ID获取Uri,请使用此代码.
private void getPhoneGalleryImages() {
Cursor cursor = cr.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null,
null, null);
cr = getActivity().getContentResolver();
if(cursor != null)
{
if (cursor.moveToFirst()) {
do {
String id = cursor.getString(cursor
.getColumnIndex(MediaStore.Images.Thumbnails._ID));
String path = cursor.getString(cursor
.getColumnIndex(MediaStore.Images.Media.DATA));
} while (cursor.moveToNext());
}
cursor.close();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4945 次 |
| 最近记录: |