如何获得歌曲的封面图片?

时间:2018-12-14 09:36:48

标签: java android image audio

我当前正在制作一个简单的移动项目,该项目正在将图像上传到我的服务器。但是我想知道,如何从我的设备的图库中获取所选歌曲的图像。简单来说,如何将我从图库(意图)中选择的封面图像放入我的imageView中,我不需要歌曲,我只是需要封面图片。没有错误,我只是问一个基于我的项目的方法。因为我不知道该怎么做。以下是我编写的代码,它根本无法运行。请帮助我找到解决方案,在此先感谢大家。

我的代码:

public class MyRecoSong extends Fragment {
   ImageView songImage;
   EditText songTitle, singer;
   Spinner sGenre;
   Button save, update;
   ListView recoSong;
   ArrayList<HashMap<String, String>> songList;
   String serverurl = "http://trinandadinantio.com";
   SongObject song[];
   TextView songID;
   String selectedPath = "";
   ProgressDialog prgDialog;
   MediaMetadataRetriever mmr = new MediaMetadataRetriever();
   byte[] rawArt;
   Bitmap art;
   BitmapFactory.Options bfo = new BitmapFactory.Options();
   List<String> Albumid = new ArrayList<>();
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       return inflater.inflate(R.layout.activity_my_reco_song, container,
               false);
   }
   @Override
   public void onActivityCreated(Bundle savedInstanceState) {
       super.onActivityCreated(savedInstanceState);
       songList = new ArrayList<>();
       songImage = getActivity().findViewById(R.id.imageView4);
       singer = getActivity().findViewById(R.id.singerTf);
       sGenre = getActivity().findViewById(R.id.spinner3);
       save = getActivity().findViewById(R.id.saveBtn);
       update = getActivity().findViewById(R.id.updateBtn);
       recoSong = getActivity().findViewById(R.id.songLv);
       Bundle bundle = getActivity().getIntent().getExtras();
       String username = bundle.getString("username");
       prgDialog = new ProgressDialog(getActivity());
       prgDialog.setCancelable(false);
       songImage.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent_upload = new Intent();
               intent_upload.setType("audio/*");
               intent_upload.setAction(Intent.ACTION_GET_CONTENT);
               startActivityForResult(intent_upload, 1);
           }
       });
   }
   private void initView() {
       songImage = getActivity().findViewById(R.id.imageView4);
       singer = getActivity().findViewById(R.id.singerTf);
       sGenre = getActivity().findViewById(R.id.spinner3);
       save = getActivity().findViewById(R.id.saveBtn);
       update = getActivity().findViewById(R.id.updateBtn);
       recoSong = getActivity().findViewById(R.id.songLv);
   }
   @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (resultCode == RESULT_OK) {
           if (requestCode == 1) {
               Cursor cursor = getAc tivity().getContentResolver().query(
                       MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                       new String[]{MediaStore.Audio.Albums._ID,
                               MediaStore.Audio.Albums.ALBUM_ART},
                       MediaStore.Audio.Albums._ID + "=?",
                       new String[
                               {String.valueOf(MediaStore.Audio.Media.ALBUM_ID)},
                       null);
               if (cursor.moveToFirst()) {
                   String path =
                           cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
                   int displayWidth =
                           getActivity().getWindowManager().getDefaultDisplay().getWidth();
                   int displayHeight =
                           getActivity().getWindowManager().getDefaultDisplay().getHeight();
                   songImage.getLayoutParams().height = displayHeight / 4;
                   songImage.getLayoutParams().width = displayWidth;
                   Bitmap imageBitmap = BitmapFactory.decodeFile(path);
                   imageBitmap = ThumbnailUtils.extractThumbnail(imageBitmap,
                           800, 600);
                   songImage.setImageBitmap(imageBitmap);
                   songImage.buildDrawingCache();
               }
           }
       }
   }
}

0 个答案:

没有答案