简体   繁体   English

如何将URI(从媒体存储区)转换为图像(位图)?

[英]How to convert URI(from mediastore) into image(Bitmap)?

hey i checked out a few questions and almost all of the questions were related to internet and mostly included methods like openConnection()and getInputStream() but mine is related to Media store so i am asking this question again here... I am able to get the Album URI by using media store and cursor but i do not know how to proceed further thanx in advance. 嘿,我检查了几个问题,几乎所有问题都与互联网有关,并且主要包括openConnection()和getInputStream()之类的方法,但是我的与媒体存储有关,所以我在这里再次提出这个问题...我可以通过使用媒体存储和光标来获取专辑URI,但我不知道该如何提前进行进一步的操作。

i tried 我试过了

Bitmap bitmap=null;
            MediaMetadataRetriever mmr = new MediaMetadataRetriever();


            byte[] rawArt = null;

            BitmapFactory.Options bfo=new BitmapFactory.Options();
            mmr.setDataSource(string);
             rawArt = mmr.getEmbeddedPicture();


             bitmap=BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);

but didn't help 但没有帮助

Edit mediastore part....... 编辑媒体存储部分。

columns  = new String[]{ android.provider.MediaStore.Audio.Albums._ID,
            android.provider.MediaStore.Audio.Albums.ALBUM,
             MediaStore.Audio.Albums.ALBUM_ART };
            cursor = 

    getActivity().managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                columns, null, null, null);


                if(cursor != null){
                    while(cursor.moveToNext()) {
                    SongDetails songs = new SongDetails();
                    songs.song= cursor.getString(1);
                    songs.Path=cursor.getString(2);
                    //songs.Album=cursor.getString(0);
                    //songs.Artist=cursor.getString(1);
                    songdetails.add(songs);
                    }}
                list=(GridView)container.findViewById(R.id.grid_view);
                adapter=new LazyAdapter( songdetails);

one such URI:"/storage/sdcard0/Android/data/com.android.providers./media/albumthumbs/138216121805 这样的URI:"/storage/sdcard0/Android/data/com.android.providers./media/albumthumbs/138216121805

位图位图= MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);

The easiest solution is to use a ContentResolver : 最简单的解决方案是使用ContentResolver

InputStream is = context.getContentResolver.openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(is);

Or, a shortcut: 或者,快捷方式:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri)

The context variable can be an Activity , Service , application context etc. context变量可以是ActivityService ,应用程序上下文等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM