我需要将一些GIF加载到自定义图库视图中。 使用mediastore只为我提供了png / jpg图像类型。 经过几个小时google,我仍然找不到答案..
我用来从mediastore加载图片的代码是:
final String sortOrder = mPreferences.getSortMode();
final Uri uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_ID, MediaStore.Images.Media.DISPLAY_NAME };
final Cursor cursor = mContext.getContentResolver().query(uri, projection, null,
null, sortOrder);
日Thnx!
答案 0 :(得分:1)
使用String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("gif");
的实例
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
来自这个ans。
答案 1 :(得分:1)
这给出了完美的结果。
private static final String[] COLUMNS_OF_INTEREST = new String[]
{
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE,
MediaStore.Video.Media.WIDTH,
MediaStore.Video.Media.HEIGHT,
MediaStore.Video.Media.DATE_ADDED
};
public void printGifUri(Context context)
{
ContentResolver cr = context.getContentResolver();
String selection = MediaStore.Images.Media.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("gif");
String[] selectionArgsPdf = new String[]{ mimeType };
Cursor gifCursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, COLUMNS_OF_INTEREST, selection,selectionArgsPdf,
MediaStore.Images.Media.DATE_ADDED + " DESC");
gifCursor.moveToFirst();
int columnIndexUri = gifCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
for (int i = 0; i < gifCursor.getCount(); i++)
Log.d("gif file uri -> ", gifCursor.getString(columnIndexUri));
}
我更喜欢使用Android LoaderManager.LoaderCallbacks,因为这会将光标加载到后台,我们会在完成后得到结果。
public class GifListManagerImpl implements LoaderManager.LoaderCallbacks<Cursor> {
Context mContext;
private static final String[] COLUMNS_OF_INTEREST = new String[]
{
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE,
MediaStore.Video.Media.WIDTH,
MediaStore.Video.Media.HEIGHT,
MediaStore.Video.Media.DATE_ADDED
};
public GifListManagerImpl(Context context)
{
mContext = context;
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
String selection = MediaStore.Images.Media.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("gif");
String[] selectionArgsPdf = new String[]{ mimeType };
return new CursorLoader(mContext, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, COLUMNS_OF_INTEREST, selection,selectionArgsPdf,
MediaStore.Images.Media.DATE_ADDED + " DESC");
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
cursor.moveToFirst();
int columnIndexUri = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
for (int i = 0; i < cursor.getCount(); i++)
Log.d("gif file uri -> ", cursor.getString(columnIndexUri));
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
答案 2 :(得分:0)
确保媒体商店扫描包含您的GIF的目录,以便通过内容提供商检索其内容。
查看此链接以获取更多信息: http://www.grokkingandroid.com/adding-files-to-androids-media-library-using-the-mediascanner/